我如何在cakephp 3中创建相关数据的分页

时间:2016-07-04 17:03:49

标签: cakephp cakephp-3.0

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

示例

我有两张桌子

  • 用户

  • User_Profiles

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

想象一下,如果我为该特定用户拥有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>

请帮忙,谢谢

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']);
}

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

答案 1 :(得分:0)

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

  控制器操作中的

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

答案 2 :(得分:0)

试试这个:

模型用户

class UsersTable extends Table {

    public function initialize(array $config) {

        $this->hasMany('UserProfiles', array(
           'foreignKey' => 'user_id'  
        ));
    }
}

模型UserProfiles

class UserProfilesTable extends Table {

    public function initialize(array $config) {

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
        ]);
    }
}

<强> UserController中

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 -->