我正在为CakePHP应用程序执行自定义分页规则。蛋糕分页创建的查询不会返回任何结果,我相信它应该。
projects ('id', 'project_leader_id'),
users ('id', 'name'),
users_projects ('id', 'project_id', 'user_id')
projects many-many users_projects on project_id,
users many-many users_projects on user_id,
users one-many projects on project_leader_id
项目:
id (1), project_leader_id (1)
id (2), project_leader_id (2)
id (3), project_leader_id (3)
用户:
id (1), name ("Peter")
id (2), name ("Joe")
id (3), name ("Mark")
users_projects:
id (1), project_id (1), user_id (2)
id (2), project_id (1), user_id (3)
id (3), project_id (2), user_id (1)
id (4), project_id (2), user_id (2)
id (5), project_id (3), user_id (3)
id (6), project_id (3), user_id (1)
SELECT Project.id, User.name
FROM projects AS Project
LEFT JOIN users_projects AS Assignment
ON Project.id = Assignment.project_id
LEFT JOIN users AS User
ON User.id = Assignment.user_id
WHERE Project.project_leader_id = 1
OR Assignment.user_id = 1
它应返回3个结果,但不会返回任何结果。
分页结构如下:
$this->paginate = array(
'Project' => array(
'joins' => array(
array(
'table' => 'users_projects',
'alias' => 'Assignment',
'type' => 'LEFT',
'conditions' => array(
'Assignment.project_id' => 'Project.id',
)
)
),
'conditions' => array(
'OR' => array(
'Assignment.user_id' => $this->Auth->User('id'),
'Project.project_leader_id' => $this->Auth->User('id')
)
)
));
查询仅返回WHERE条件中指定的包含Project.project_leader_id
的项目,并且不考虑Assignment.user_id
条件。
答案 0 :(得分:0)
我发现了问题所在。 'join'键下的分页变量中的'conditions'子句必须在没有双箭头的情况下构造。
更改
'conditions' => array('Assignment.project_id' => 'Project.id')
到
'conditions' => array('Assignment.project_id = Project.id')
解决了这个问题。
初始查询如下所示:
LEFT JOIN `consultants_assignments` AS `consultantsAssignment` ON
(`consultantsAssignment`.`project_id` = `Project.id`)
并在更改后:
LEFT JOIN `consultants_assignments` AS `consultantsAssignment` ON
(`consultantsAssignment`.`project_id` = `Project`.`id`)