2016-07-04 68 views
1

我正在使用CakePHP 3,并且我正在尝试为相关数据创建分页,换句话说,所有记录都属于第一个表格。我怎么能在cakephp中创建相关数据的分页3

我有两个表

  • 用户

  • User_Profiles

用户的hasMany配置文件,所以当我上的任何用户点击它会显示我与V相关的个人资料浏览功能,

想象一下,如果我对该特定用户有10000个配置文件,将很难显示所有1000个配置文件,我如何为他们分页?

这是我的控制器

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

    ]); 

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

从功能,我们可以看到我们有,如果我对每个表10000记录也将可以在视图模板而不分页3个表是相关的,这是真的很难加载页面,甚至阅读所有,

这是我的看法

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

请帮帮忙,谢谢

+0

'我有两个tables'你的代码使用了完全不同的名字 - 请desribe你的实际问题,而不是它的简单化版本 - 即作出描述一致与问题中的代码。 – AD7six

回答

0

也许这是你在找什么:

在控制器动作

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

对不起,我不明白我应该把这个?看看我的浏览功能在我的控制器 –

+0

>在您的情况: 公共职能视图($ ID = NULL){ $限制= 10; //限制每个关联10条记录 $ 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']); } 我希望这能解决你的问题:) –

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

我希望这会解决您的问题:)