CakePHP版本: 4.0.1
Introduction
这个问题源于另一个这里问题,在这个问题中,一个不理想的解决方案是重定向回它自己。不幸的是,我正在测试的控制器没有任何相关的列,所以这个新的问题没有被识别出来。
我在烹饪书中提到了这。
希望下面的代码能够让问题重现。
联系人表
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;
}联系人控制器
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'));
}发生了什么
该页在框架中显示类型错误。
C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper.php
strtolower()期望参数1是字符串,bool给定
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来自error.log
2020-06-01 10:33:20错误: C:\xampp\htdocs\crm\vendor\cakephp\cakephp\src\View\Helper\PaginatorHelper.php strtolower()期望参数1为string,在第264行中以TypeError表示
堆栈跟踪:
请求网址: /contacts
参考网址:https://localhost/crm/welcome
问题
为什么显示类型错误而不是用account_name上的排序加载索引模板?
谢谢Z。
编辑
我刚刚尝试了一个新的安装
作曲家自我更新和作曲家创建-项目-偏好-dist cakephp/app:4.* crm
和烤熟的用户,联系人和账户。
新版本为4.0.8,我在新项目中添加了上面的代码,但不幸的是,错误类型完全相同。
如果排序在相同的表上,即: Contacts.last_name姓氏上有排序,但如果我将其更改为关联的表Accounts.account_name,则会显示类型错误。
发布于 2020-06-03 09:37:19
直接使用分页器时,必须在配置中传递排序字段白名单。
$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上。
https://stackoverflow.com/questions/62130796
复制相似问题