Cakephp 3 使用包含时的动态限制参数

Cakephp 3 Dynamic limit parameter when using contain

CakePHP 版本 3.5.5

我的最终目标是为用户提供更改通过索引视图上的 select 列表显示的结果数量的功能。我还需要按 area_name asc.

对初始页面加载进行排序

// 我做了什么

我更改了我规定的限制参数,如下所示。

// AREAS CONTROLLER 

public $paginate = [
    'sortWhitelist' => [
       'Areas.area_name', 'Users.first_name', 'Users.last_name'
    ]
    //'limit' => 1, // REMOVED FROM HERE
    //'order' => [ // REMOVED FROM HERE
        //'Areas.area_name' => 'asc'
    //]
];

public function index()
{
    $query = $this->Areas->find('all')        
        ->contain([
            'Users'
        ])
        ->where(['Areas.status' => 1]);

    $limit = 1;
    $this->paginate = [
        'order' => ['Areas.area_name' => 'asc'], // ADDED HERE
        'limit' => $limit // ADDED HERE
     ];        

     $this->set('areas', $this->paginate($query));
}

我声明分页排序链接如下:

// AREAS INDEX VIEW

<?= $this->Paginator->sort('Areas.area_name', __('Area Name')) ?>
<?= $this->Paginator->sort('Users.first_name', __('First Name')) ?>
<?= $this->Paginator->sort('Users.last_name', __('Last Name')) ?>

// 结果

上面的代码适用于应用程序中所有不使用 contain 的索引方法,但是当我在此处实施此解决方案时,一切正常除了我无法对关联数据进行排序 - IE:用户名字和姓氏?

=========================================== ============================

我尝试了什么

// 尝试 1

我在 public $paginate class 上方添加了一个初始化方法,例如:

public function initialize()
{
    $limit = 1;      
}

public $paginate = [
    'sortWhitelist' => [
        'Areas.area_name', 'Users.first_name', 'Users.last_name'
    ]
    'limit' => $limit,
    'order' => [
        'Areas.area_name' => 'asc'
    ]
];

public function index()
{
    $query = $this->Areas->find('all')        
        ->contain([
            'Users'
        ])
       ->where(['Areas.status' => 1]);

    $this->set('areas', $this->paginate($query));
}

和我留下的观点一样。

// 尝试 1 的结果

语法错误,意外的“'limit'”(T_CONSTANT_ENCAPSED_STRING),第 36 行需要“]”,即 'limit' => $limit,

=========================================== ============================

// 尝试 2

我尝试将限制参数和订单数组添加到查询中,例如:

public function index()
{
    $limit = 1;
    $query = $this->Areas->find('all')        
        ->contain([
            'Users'
        ])
        ->where(['Areas.status' => 1])
        ->order(['Areas.area_name' => 'asc'])            
        ->limit($limit);

     $this->set('areas', $this->paginate($query));

}

// 尝试 2 的结果

结果集未按 area_name 排序且不限于 1 个结果。

=========================================== ============================

// 尝试 3

然后我更改了查询并尝试了以下操作以查看是否可以使动态限制起作用:

$limit = 1;
$query = $this->Areas->find('all') 
    ->contain('Users', function ($q) {
        return $q
        //->order('Areas.area_name' => 'asc'),
        ->limit($limit);
    })
    ->where(['Areas.status' => 1]);

    $this->set('areas', $this->paginate($query));

// 尝试 3 的结果

结果集不限于 1 个结果。

=========================================== ============================

附加信息

// USERS TABLE

$this->hasOne('Areas', [
    'foreignKey' => 'user_id'
]);

// AREAS TABLE

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

我搜索了以下食谱部分(分页、查询生成器、检索数据和结果集以及关联 - 将表链接在一起),但我找不到实现此功能的方法,因此非常感谢您的帮助。

非常感谢。 Z.

您正在覆盖 index() 方法中的 $paginate 属性,因此包括白名单在内的设置将丢失。

改为直接设置键:

$this->paginate['order'] = ['Areas.area_name' => 'asc'];
$this->paginate['limit'] = $limit;