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

Pra*_*tel 7 sorting paginator cakephp-3.0

我有两个模型用户和角色

这里"Roles hasMany Users"和"Users areTo Roles"

当用户保存时,我们还要求保存用户的角色和记录.

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

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

UsersController:

public function index() {
     $this->paginate = [
                'contain' => ['Roles'],
                'conditions' => [
                    'Users.user_type <>' => 1
                ]
            ];

            $this->set('users', $this->paginate($this->Users));
            $this->set('_serialize', ['users']);
}
Run Code Online (Sandbox Code Playgroud)

index.ctp

 <tr>
                <th><?php echo $this->Paginator->sort('firstname',__('First Name')) ?></th>
                <th><?php echo $this->Paginator->sort('lastname',__('Last Name')) ?></th>
                <th><?php echo $this->Paginator->sort('email',__('Email Address')) ?></th>
                <th><?php echo $this->Paginator->sort('Roles.name',__('Role Associated')) ?></th>
                <th><?php echo $this->Paginator->sort('status',__('status')) ?></th>
                <th class="actions"><?php echo __('action') ?></th>
            </tr>
Run Code Online (Sandbox Code Playgroud)

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

Sai*_*ain 18

你只需要使用它:

$this->paginate = [
    'sortWhitelist'=>['Roles.name']
];
Run Code Online (Sandbox Code Playgroud)

  • 更准确一点:您还必须包含要排序的所有其他列.在Controller-Action`代码$ this-> paginate = ['contains'=> ['Roles'],'sortWhitelist'=> ['Roles.name','firstname','lastname','email','status ']]; `[link](http://book.cakephp.org/3.0/en/controllers/components/pagination.html#control-which-fields-used-for-ordering) (3认同)

小智 1

用户控制器.php

 $this->paginate = [
            'contain' => ['Roles'],
            'conditions' => [
                'Users.user_type <>' => 1
            ],
            'order' => ['Users.role_id' => 'ASC']
        ];
Run Code Online (Sandbox Code Playgroud)

使用 'order' => ['Models.field' => 'ASC/DESC']