直接使用分页器并在关联列上定义排序无效 [英] Using the paginator directly and defining the sort on an associated column not working

查看:63
本文介绍了直接使用分页器并在关联列上定义排序无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CakePHP版本:4.0.1

CakePHP Version: 4.0.1

简介

出现此问题由于另一个问题,我此处理想的解决方案是将其重定向回自身。不幸的是,我正在测试的控制器没有任何关联的列,因此未发现此新问题。

This problem follows on from another problem I had here where a not ideal solution was to redirect back to itself. Unfortunately the controller I was testing didn't have any associated columns so this new problem was not identified.

我引用了在食谱中

希望下面的代码可以重现该问题。

Hopefully the below code will allow the problem to be reproduced.

联系人表 >

public function initialize(array $config): void
{
    parent::initialize($config);

    $this->setTable('contacts');

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

    $this->belongsTo('Accounts', [
        'foreignKey' => 'account_id'
    ]);

}

// Custom Finder
public function findSuperuserAllContacts(Query $query, array $options): object
{
    $query
        ->where(['Contacts.status' => $options['status']])
        ->andWhere([
            'Users.client_id' => $options['client_id'],
            'Users.status' => 1
        ]);

    return $query;
}

Contacts Controller

public $paginate = [
    'sortWhitelist' => [
       'Contacts.first_name',
       'Contacts.last_name',
       'Accounts.account_name',
       'Users.first_name',
       'Users.last_name',
       'Contacts.primary_tel',
       'Contacts.mobile',
       'Contacts.email'
    ]
];

public function index() {

    $query = (object) $this->Contacts->find('superuserAllContacts', [
        'contain' => ['Users', 'Accounts'],
        'status' => 1,
        'client_id' => 1001
    ]);

    $page = '';
    $sort = 'Accounts.account_name';
    $direction = 'asc';

    $config = $this->paginate = [                
        'page' => $page,
        'sort' => $sort,
        'direction' => $direction,
        'limit' => 10
    ];        

    $contacts = $this->Paginator->paginate($query, $config);
    $this->set(compact('contacts'));

}






发生了什么

页面在框架中显示类型错误。

The page displays a type error in the framework.

C: ampxampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper.php

C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper.php

strtolower()期望参数1为字符串,给定布尔值

strtolower() expects parameter 1 to be a string, bool given

public function sortDir(?string $model = null, array $options = []): string
{
    $dir = null;

    if (empty($options)) {
        $options = $this->params($model);
    }

    if (isset($options['direction'])) {

        debug($options['direction']);  // THIS IS FALSE NOT asc or desc?

        $dir = strtolower($options['direction']);
    }

    if ($dir === 'desc') {
        return 'desc';
    }

    return 'asc';
}






Stacktrace from error.log

2020-06-01 10:33:20错误:[TypeError] strtolower()期望参数1为字符串,布尔值在C:第264行的xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper.php

2020-06-01 10:33:20 Error: [TypeError] strtolower() expects parameter 1 to be string, bool given in C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper.php on line 264

堆栈跟踪:

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper .php:264

-C:\xampp\htdocs\crm\templates\Contacts\index.php:5

-C:\ xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\View.php:1164

-C:\xampp\htdocs\crm\ \vendor\cakephp\cakephp\src\View\View.php:1125

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp \src\View\View.php:750

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Controller\ Controller.php:691

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Controller\Controller.php:533

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Controller\ControllerFactory.php:79

-C:\ xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\BaseApplication.php:229

-C:\xampp\htdocs\crm\ \vendor\cakephp\cakephp\src\Http\Runner.php:77

-C:\xampp\htdocs\crm\vendor\cakephp\authentication \src\Middleware\AuthenticationMiddleware.php:122

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\ Runner.php:73

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\I18n\Middleware\LocaleSelectorMiddleware.php:70

-C:xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:77

-C:\xampp\ htdocs\crm\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php:132

-C:\xampp\htdocs\crm\ \vendor\cakephp\cakephp\src\Http\Runner.php:73

-C:xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:58

-C:\ xampp\htdocs\crm\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php:162

-C:\xampp\htdocs\ \crm\vendor\cakephp\cakephp\src\Http\Runner.php:73

-C:\xampp\htdocs\crm\vendor\cakephp \cakephp\src\Routing\Middleware\AssetMiddleware.php:68

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\ src\Http\Runner.php:73

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Error\Middleware\ HandlerErrorHandlerMiddleware.php:118

-C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73

-C:xampp\htdocs\crm\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php:60

-C :\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73

-C:\xampp\htdocs\ \crm\vendor\cakephp\cakephp\src\Http\Runner.php:58

-C:\xampp\ht docs\crm\vendor\cakephp\cakephp\src\Http\Server.php:90

-C:\xampp\htdocs\crm\webroot\ \index.php:40

Stack Trace:
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper.php:264
- C:\xampp\htdocs\crm\templates\Contacts\index.php:5
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\View.php:1164
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\View.php:1125
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\View.php:750
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Controller\Controller.php:691
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Controller\Controller.php:533
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Controller\ControllerFactory.php:79
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\BaseApplication.php:229
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:77
- C:\xampp\htdocs\crm\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php:122
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\I18n\Middleware\LocaleSelectorMiddleware.php:70
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:77
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php:132
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:58
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php:162
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php:68
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php:118
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\crm\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php:60
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Runner.php:58
- C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\Http\Server.php:90
- C:\xampp\htdocs\crm\webroot\index.php:40

请求URL:/ contacts

引用URL: https:// localhost / crm / welcome

Request URL: /contacts
Referer URL: https://localhost/crm/welcome

问题

为什么会显示类型错误,而不是使用account_name上的排序方式加载索引模板?

Why is a type error displayed instead of loading the index template with the sort on the account_name?

感谢Z。

EDIT

我刚刚尝试了

composer自更新&& composer create-project --prefer-dist cakephp / app:4. * crm

composer self-update && composer create-project --prefer-dist cakephp/app:4.* crm

并烘焙用户,联系人和帐户。

and baked users, contacts and accounts.

新版本为4.0.8,我在新项目中添加了上面的代码,但不幸的是类型错误完全相同。

The new version is 4.0.8 and I added the code I have above in the new project but unfortunatley got exactly the same type error.

如果sort在同一个表上,即:Contacts.last_name姓氏在上面排序,但是如果我将其更改为关联的表Accounts.account_name,则会显示类型错误。

If the sort is on the same table, ie: Contacts.last_name the last name has the sort on it but if I change it to the associated table Accounts.account_name the type error is displayed.

推荐答案

直接使用分页器时,必须在配置中传递排序字段白名单。

The sort field whitelist must be passed in the config when using the paginator directly.

$config = $this->paginate = [
    'sortWhitelist' => [
        'Contacts.first_name',
        'Contacts.last_name',
        'Accounts.account_name',
        'Users.first_name',
        'Users.last_name',
        'Contacts.primary_tel',
        'Contacts.mobile',
        'Contacts.email'
    ],
    'page' => $page,
    'sort' => $sort,
    'direction' => $direction,
    'limit' => 10
];

$contacts = $this->Paginator->paginate($query, $config);
$this->set(compact('contacts'));

排序现在显示在account_name上。

The sort is now displayed on the account_name.

这篇关于直接使用分页器并在关联列上定义排序无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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