【问题标题】:how i can create pagination for related data in cakephp 3我如何在 cakephp 3 中为相关数据创建分页
【发布时间】:2023-04-10 22:00:01
【问题描述】:

我正在使用 Cakephp 3,我正在尝试为相关数据创建分页,换句话说,所有记录都属于第一个表。

示例

我有两张桌子

  • 用户

  • User_Profiles

用户有很多个人资料,所以当我点击任何用户时,它会在查看功能中显示相关的个人资料,

想象一下,如果我有 10000 个特定用户的个人资料,那么很难显示所有 1000 个个人资料,我如何为他们分页?

这是我的控制器

public function view($id = null)
{
    $distributor = $this->Distributors->get($id, [
        'contain' => ['DistributorUsers', 'Participants', 'Payments']

    ]);

    $this->set('distributor', $distributor);
    $this->set('_serialize', ['distributor']);
}

从该函数我们可以看到我们有 3 个表是相关的,如果我在每个表上都有 10000 条记录,它将在视图模板上可用而无需分页,这真的很难加载页面甚至阅读所有内容,

这是我的看法

<table class="table table-condensed table-hover">
        <thead>
                        <tr>
                            <th colspan="3"> <?= h($distributor->name) ?> </th>
                        </tr>
         </thead>

     <tbody>
        <tr>
            <td><?= __('Name') ?></th>
            <td><?= h($distributor->name) ?></td>
        </tr>
        <tr>
            <td><?= __('Location') ?></th>
            <td><?= h($distributor->location) ?></td>
        </tr>
        <tr>
            <td><?= __('Phone') ?></th>
            <td><?= h($distributor->phone) ?></td>
        </tr>
        <tr>
            <td><?= __('Id') ?></th>
            <td><?= $this->Number->format($distributor->id) ?></td>
        </tr>
        <tr>
            <td><?= __('Created') ?></th>
            <td><?= h($distributor->created) ?></td>
        </tr>
        <tr>
            <td><?= __('Active') ?></th>
            <td><?= $distributor->active ? __('Yes') : __('No'); ?></td>
        </tr>
    </table>


    <div class="related">
        <h4><?= __('Related Distributor Users') ?></h4>
        <?php if (!empty($distributor->distributor_users)): ?>


        <table class="table table-hover" id="sample-table-1">
            <thead>
            <tr>
                <th><?= __('Id') ?></th>
                <th><?= __('Level') ?></th>
                <th><?= __('Username') ?></th>
                <th><?= __('Email') ?></th>
                <th><?= __('Created') ?></th>
                <th class="actions"><?= __('Actions') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($distributor->distributor_users as $distributorUsers): ?>
            <tr>
                <td><?= h($distributorUsers->id) ?></td>

                <td><?= h($distributorUsers->level) ?></td>
                <td><?= h($distributorUsers->username) ?></td>

                <td><?= h($distributorUsers->email) ?></td>

                <td><?= h($distributorUsers->created) ?></td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['controller' => 'DistributorUsers', 'action' => 'view', $distributorUsers->id]) ?>
                    <?= $this->Html->link(__('Edit'), ['controller' => 'DistributorUsers', 'action' => 'edit', $distributorUsers->id]) ?>
                    <?= $this->Form->postLink(__('Delete'), ['controller' => 'DistributorUsers', 'action' => 'delete', $distributorUsers->id], ['confirm' => __('Are you sure you want to delete # {0}?', $distributorUsers->id)]) ?>
                </td>
            </tr>
            <?php endforeach; ?>
             </tbody>
        </table>

        <?php endif; ?>



    <div class="related">
        <h4><?= __('Related Participants') ?></h4>
        <?php if (!empty($distributor->participants)): ?>


        <table class="table table-hover" id="sample-table-1">
            <thead>
            <tr>
                <th><?= __('Id') ?></th>
                <th><?= __('Age') ?></th>
                <th><?= __('Dateofbirth') ?></th>
                <th><?= __('Gender') ?></th>
                <th><?= __('Address') ?></th>
                <th><?= __('Mobile') ?></th>
                <th><?= __('Relatives Phone') ?></th>
                <th><?= __('Has Disease') ?></th>
                <th><?= __('Disease') ?></th>
                <th><?= __('Ip Address') ?></th>
                <th><?= __('Distributor Id') ?></th>
                <th><?= __('Event Id') ?></th>
                <th><?= __('Is First Time') ?></th>
                <th><?= __('Is Payment Done') ?></th>
                <th><?= __('Confirm Attendance') ?></th>
                <th><?= __('First Name') ?></th>
                <th><?= __('Second Name') ?></th>
                <th><?= __('Third Name') ?></th>
                <th><?= __('Last Name') ?></th>
                <th><?= __('Lockbox Number') ?></th>
                <th><?= __('Created') ?></th>
                <th class="actions"><?= __('Actions') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($distributor->participants as $participants): ?>
            <tr>
                <td><?= h($participants->id) ?></td>
                <td><?= h($participants->age) ?></td>
                <td><?= h($participants->dateofbirth) ?></td>
                <td><?= h($participants->gender) ?></td>
                <td><?= h($participants->address) ?></td>
                <td><?= h($participants->mobile) ?></td>
                <td><?= h($participants->relatives_phone) ?></td>
                <td><?= h($participants->has_disease) ?></td>
                <td><?= h($participants->disease) ?></td>
                <td><?= h($participants->ip_address) ?></td>
                <td><?= h($participants->distributor_id) ?></td>
                <td><?= h($participants->event_id) ?></td>
                <td><?= h($participants->is_first_time) ?></td>
                <td><?= h($participants->is_payment_done) ?></td>
                <td><?= h($participants->confirm_attendance) ?></td>
                <td><?= h($participants->first_name) ?></td>
                <td><?= h($participants->second_name) ?></td>
                <td><?= h($participants->third_name) ?></td>
                <td><?= h($participants->last_name) ?></td>
                <td><?= h($participants->lockbox_number) ?></td>
                <td><?= h($participants->created) ?></td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['controller' => 'Participants', 'action' => 'view', $participants->id]) ?>
                    <?= $this->Html->link(__('Edit'), ['controller' => 'Participants', 'action' => 'edit', $participants->id]) ?>
                    <?= $this->Form->postLink(__('Delete'), ['controller' => 'Participants', 'action' => 'delete', $participants->id], ['confirm' => __('Are you sure you want to delete # {0}?', $participants->id)]) ?>
                </td>
            </tr>
            <?php endforeach; ?>
             </tbody>
        </table>

        <?php endif; ?>



    <div class="related">
        <h4><?= __('Related Payments') ?></h4>
        <?php if (!empty($distributor->payments)): ?>


        <table class="table table-hover" id="sample-table-1">
            <thead>
            <tr>
                <th><?= __('Id') ?></th>
                <th><?= __('Participant Id') ?></th>
                <th><?= __('Distributor Id') ?></th>
                <th><?= __('Event Id') ?></th>
                <th><?= __('Distributor User Id') ?></th>
                <th><?= __('Amount') ?></th>
                <th><?= __('Created') ?></th>
                <th><?= __('Ip Address') ?></th>
                <th class="actions"><?= __('Actions') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($distributor->payments as $payments): ?>
            <tr>
                <td><?= h($payments->id) ?></td>
                <td><?= h($payments->participant_id) ?></td>
                <td><?= h($payments->distributor_id) ?></td>
                <td><?= h($payments->event_id) ?></td>
                <td><?= h($payments->distributor_user_id) ?></td>
                <td><?= h($payments->amount) ?></td>
                <td><?= h($payments->created) ?></td>
                <td><?= h($payments->ip_address) ?></td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['controller' => 'Payments', 'action' => 'view', $payments->id]) ?>
                    <?= $this->Html->link(__('Edit'), ['controller' => 'Payments', 'action' => 'edit', $payments->id]) ?>
                    <?= $this->Form->postLink(__('Delete'), ['controller' => 'Payments', 'action' => 'delete', $payments->id], ['confirm' => __('Are you sure you want to delete # {0}?', $payments->id)]) ?>
                </td>
            </tr>
            <?php endforeach; ?>
             </tbody>
        </table>

        <?php endif; ?>


</div>
</div></div>

请帮忙,谢谢

【问题讨论】:

  • i have two tables 您的代码使用完全不同的名称 - 请描述您的实际问题,而不是简化版本 - 即使描述与问题中的代码一致。

标签: cakephp cakephp-3.0


【解决方案1】:

在你的情况下:

public function view($id = null){
    $limit = 10; //limit 10 record for each assosiation
    $this->paginate = [
        'DistributorUsers' => function ($q){ return $q->limit($limit); },
        'Participans' => function ($q){ return $q->limit($limit); },
        'Payments' => function ($q){ return $q->limit($limit); }
    ];
    $distributor = $this->paginate($this->Distributors);
    $this->set('distributor', $distributor);
    $this->set('_serialize', ['distributor']);
}

我希望这能解决你的问题:)

【讨论】:

    【解决方案2】:

    试试这个:

    模型用户

    class UsersTable extends Table {
    
        public function initialize(array $config) {
    
            $this->hasMany('UserProfiles', array(
               'foreignKey' => 'user_id'  
            ));
        }
    }
    

    模型用户配置文件

    class UserProfilesTable extends Table {
    
        public function initialize(array $config) {
    
            $this->belongsTo('Users', [
                'foreignKey' => 'user_id',
            ]);
        }
    }
    

    用户控制器

    public function view($id = null) {
    
        if(is_null($id)) {
            $id = $this->request->param('id');
        }
    
        //Get User
        $user = $this->Users->get($id);
    
        //Get Paginated User Profiles by User id
        $this -> paginate['contain'] = ['Users'];
        $this -> paginate['conditions'] = ['UserProfiles.user_id =' => $id];
        $this -> paginate['limit'] = 8;
        $this -> paginate['order'] = ['UserProfiles.created' => 'desc'];
    
        //Url paginator e.g.: /user/153/?page=2
        $paginateUrl = ['id'=> $this->request->param('id')];
    
        /*If you use slugs in your route*/
        //Url paginator e.g.: /user/153/userSlug.html?page=2
        //$paginateUrl = ['id'=> $this->request->param('id'), 'slug' => $this->request->param('slug')];
    
        $this->set(compact('paginateUrl'));
        $this->set('UserProfiles', $this->paginate($this->UserProfiles));
        $this->set(compact('user'));
    }
    

    查看

    <?php foreach($userProfiles as $userProfile): ?>
        ...
    <?php endforeach; ?> 
    
    <!-- Pagination -->
    <div class="row text-center">
        <div class="col-lg-12">
            <ul class="pagination">
                <?= $this->Paginator->options(['url' => $paginateUrl])?>
                <?= $this->Paginator->prev('< ' . __('previous')) ?>
                <?= $this->Paginator->numbers() ?>
                <?= $this->Paginator->next(__('next') . ' >') ?>
            </ul>       
        </div>
    </div>
    <!-- /.Pagination -->
    

    【讨论】:

      【解决方案3】:

      也许这就是你要找的东西:

      在控制器动作中:

      $assosiationLimit = 10 //limit display data for User Profile
      $this->paginate = [
           'contain' => [
                'UserProfiles' => function($q){
                     return $q->limit($assosiationLimit);
                }
           ]
      ];
      $users = $this->paginate($this->Users);
      

      【讨论】:

      • 对不起,我不明白我应该把这个放在哪里?在我的控制器中查看我的视图功能
      • > 在您的情况下: public function view($id = null){ $limit = 10; //每个关联限制 10 条记录 $this->paginate = [ 'DistributorUsers' => function ($q){ return $q->limit($limit); }, '参与者' => function ($q){ return $q->limit($limit); }, '支付' => function ($q){ return $q->limit($limit); } ]; $distributor = $this->paginate($this->Distributors); $this->set('distributor', $distributor); $this->set('_serialize', ['distributor']); } 我希望这能解决你的问题 :)
      猜你喜欢
      • 2011-06-05
      • 1970-01-01
      • 2023-03-04
      • 2016-09-16
      • 2020-05-17
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多