正如我在cakephp 3.x中所做的那样,我可以通过一个有计数的行对结果进行排序,因为我看不到表单,CakePHP 2.x中非常类似的东西没有问题。 缺少什么能够在CakePHP 3.x中做同样的事情? 我的测试是PHP 7.1 WAMP中的CakePHP v3.3.10
除了最终的 count_users 之外,所有类型的分页都能正常工作,当我点击它时不会出错,只是不对数据进行排序。
HTML ctp
<?= $this->Paginator->sort('count_users',_('N° Users')); ?>
控制器
$this->Licensees->schema()
->addColumn('count_users', [
'type' => 'integer',
]);
$this->Licensees->aliasField('count_users');
$where = [
'recursive'=>-1,
'fields' => [
'Licensees.id',
'Licensees.name',
'Licensees.created',
'Licensees.modified',
'Licensees__count_users' => 'count(LicenseesUsers.licensees_id)',
// 'count_users' => 'count(LicenseesUsers.licensees_id)',
// 'Licensees.count_users' => 'count(LicenseesUsers.licensees_id)',
],
'sortWhitelist' => ['name','count_users','created','modified'],
'join' => [
'LicenseesUsers' => [
'table' => 'licensees_users',
'type' => 'LEFT',
'conditions' => [
'LicenseesUsers.licensees_id = Licensees.id'
],
],
],
'group' => 'Licensees.id'
];
// Set pagination
$this->paginate = $where;
// Get data
$licensees = $this->paginate($this->Licensees);
显示错误排序分页字段count_users
object(Cake\ORM\Query) {
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT Licensees.id AS `Licensees__id`, Licensees.name AS `Licensees__name`, Licensees.created AS `Licensees__created`, Licensees.modified AS `Licensees__modified`, count(LicenseesUsers.licensees_id) AS `Licensees__count_users`, count(LicenseesUsers.licensees_id) AS `count_users` FROM licensees Licensees LEFT JOIN licensees_users LicenseesUsers ON LicenseesUsers.licensees_id = Licensees.id LEFT JOIN users Users ON Users.id = LicenseesUsers.users_id GROUP BY Licensees.id ORDER BY Licensees.count_users asc LIMIT 50 OFFSET 0',
'params' => [],
'defaultTypes' => [
'Licensees__id' => 'uuid',
'Licensees.id' => 'uuid',
'id' => 'uuid',
'Licensees__name' => 'string',
'Licensees.name' => 'string',
'name' => 'string',
'Licensees__active' => 'boolean',
'Licensees.active' => 'boolean',
'active' => 'boolean',
'Licensees__deleted_at' => 'datetime',
'Licensees.deleted_at' => 'datetime',
'deleted_at' => 'datetime',
'Licensees__created' => 'datetime',
'Licensees.created' => 'datetime',
'created' => 'datetime',
'Licensees__modified' => 'datetime',
'Licensees.modified' => 'datetime',
'modified' => 'datetime',
'Licensees__count_users' => 'integer',
'Licensees.count_users' => 'integer',
'count_users' => 'integer'
],
'decorators' => (int) 0,
'executed' => false,
'hydrate' => true,
'buffered' => true,
'formatters' => (int) 0,
'mapReducers' => (int) 0,
'contain' => [],
'matching' => [],
'extraOptions' => [
'recursive' => (int) -1,
'scope' => null,
'sortWhitelist' => [
(int) 0 => 'name',
(int) 1 => 'count_users',
(int) 2 => 'created',
(int) 3 => 'modified'
],
'whitelist' => [
(int) 0 => 'limit',
(int) 1 => 'sort',
(int) 2 => 'page',
(int) 3 => 'direction'
]
],
'repository' => object(App\Model\Table\LicenseesTable) {
'registryAlias' => 'Licensees',
'table' => 'licensees',
'alias' => 'Licensees',
'entityClass' => 'App\Model\Entity\Licensee',
'associations' => [
[maximum depth reached]
],
'behaviors' => [
[maximum depth reached]
],
'defaultConnection' => 'default',
'connectionName' => 'default'
}
}
答案 0 :(得分:1)
正如评论中所提到的,你必须使用count_users
变体,即没有任何别名,这通常会正常工作。
因为你已经在你的模式(count_users
)中创建了$this->Licensees->schema()->addColumn()
列部分,所以会破坏你的东西,这将导致CakePHP将其视为真正的列,因此它将在内部别名列名称将导致ORDER BY
子句使用Licensees.count_users
,这将不起作用,因为不存在此列(您不能将计算列添加到现有别名)。
如果您需要将计算列视为特定类型,则通过创建和分页正确的查询对象,或者通过动态修改查询(例如查找程序)来修改(select)类型映射,或通过Model.beforeFind
事件。
// use getSelectTypeMap() as of CakePHP 3.4
$query->selectTypeMap()->addDefaults([
'count_users' => 'integer'
]);
另见
答案 1 :(得分:0)
感谢ndm ,这就是你所说的。
问题将在以下几行中进行评论。
<强>控制器:强>
// $this->Licensees->schema()
// ->addColumn('count_users', [
// 'type' => 'integer',
// ]);
// $this->Licensees->aliasField('count_users');
$where = [
'recursive'=>-1,
'fields' => [
'Licensees.id',
'Licensees.name',
'Licensees.created',
'Licensees.modified',
// 'Licensees__count_users' => 'count(LicenseesUsers.licensees_id)',
'count_users' => 'count(LicenseesUsers.licensees_id)',
],
'sortWhitelist' => ['name','count_users','created','modified'],
'join' => [
'LicenseesUsers' => [
'table' => 'licensees_users',
'type' => 'LEFT',
'conditions' => [
'LicenseesUsers.licensees_id = Licensees.id'
],
],
],
'group' => 'Licensees.id'
];
// Set pagination
$this->paginate = $where;
// Get data
$licensees = $this->paginate($this->Licensees);