我尝试在cakephp 3.6中编写此查询:
SELECT `Group`.`id`, `Group`.`name`, `Group`.`created`, `Group`.`modified`, (SELECT count(*)
FROM `positions` where Group.id = positions.group_id) AS "count" FROM `groups` AS `Group`
我可以使用原始查询来获取结果,但是如何使用ORM \ Query来实现呢?
答案 0 :(得分:0)
有些喜欢这个。
$query = $this->Groups->find();
$query->select([
'Groups.id',
'Groups.name',
'Groups.created',
'Groups.modified',
'count' => $query->func()->count('Positions.id')
])->leftJoinWith('Positions')
->group('Groups.id');
答案 1 :(得分:0)
最后我找到了解决方法:
identitytoolkit
查看日志时,我得到:
$position = TableRegistry::getTableLocator()->get('Positions');
$q = $position->find();
$q->select([$q->func()->count('*')])
->where(['Positions.group_id = Groups.id']);
$query = $this->Groups->find()
->select(['id', 'name', 'created', 'modified' , 'count' => $q]);