我正在为 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")
用户项目:
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')
)
)
));
更新
该查询仅返回包含Project.project_leader_id
WHERE 条件中指定的项目,并且不考虑Assignment.user_id
条件。