Cakephp 3.x另一个模型的排序不起作用

前端之家收集整理的这篇文章主要介绍了Cakephp 3.x另一个模型的排序不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个型号用户&角色

这里“Roles hasMany Users”和“Users areTo Roles”

用户保存时,我们也会询问用户的角色&记录保存.

问题:
我有列firstname,lastname,roles的用户列表.每个&每列都有排序,但角色排序不起作用.

角色表包含​​角色名称的“名称”字段.
我在下面提到了链接,但它对我不起作用.
Pagination Sort in Cakephp 3.x

UsersController:

  1. public function index() {
  2. $this->paginate = [
  3. 'contain' => ['Roles'],'conditions' => [
  4. 'Users.user_type <>' => 1
  5. ]
  6. ];
  7.  
  8. $this->set('users',$this->paginate($this->Users));
  9. $this->set('_serialize',['users']);
  10. }

index.ctp

  1. <tr>
  2. <th><?PHP echo $this->Paginator->sort('firstname',__('First Name')) ?></th>
  3. <th><?PHP echo $this->Paginator->sort('lastname',__('Last Name')) ?></th>
  4. <th><?PHP echo $this->Paginator->sort('email',__('Email Address')) ?></th>
  5. <th><?PHP echo $this->Paginator->sort('Roles.name',__('Role Associated')) ?></th>
  6. <th><?PHP echo $this->Paginator->sort('status',__('status')) ?></th>
  7. <th class="actions"><?PHP echo __('action') ?></th>
  8. </tr>

让我知道你有任何解决方案.

你只需要使用这个:
  1. $this->paginate = [
  2. 'sortWhitelist'=>['Roles.name']
  3. ];

猜你在找的PHP相关文章