Cakephp 3.x另一个模型的排序不起作用 [英] Cakephp 3.x Sorting of another model is not working

查看:82
本文介绍了Cakephp 3.x另一个模型的排序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种型号的用户&角色

I have two models Users & Roles

角色有很多用户"和用户属于角色"

Here "Roles hasMany Users" and "Users belongsTo Roles"

在用户保存后,我们还会询问用户的角色&记录已保存.

When the user saved we're also asking user's role & record saved.

问题: 我有列名,姓氏,角色的用户列表.每个&每列都有排序,但是在角色上排序不起作用.

Problem : I have list of users with column firstname, lastname,roles. Each & Every column has sorting but on roles sorting is not working.

角色表包含​​角色名称的名称"字段. 我已经提到了以下链接,但它对我不起作用. 在Cakephp 3.x中进行分页排序

Role Table contains "name" field for Role name. I have referred below link but it doesn't working for me. Pagination Sort in 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']);
}

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>

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

Let me know any solution you have.

推荐答案

您只需要使用以下内容:

You just have to use this:

$this->paginate = [
    'sortWhitelist'=>['Roles.name']
];

这篇关于Cakephp 3.x另一个模型的排序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆