我遇到查询问题:
$userInProjects = $this->Timesheet->RegularPost->UserInProject->find('all', array('conditions' => array('UserInProject.user_id' => $id)));
结果数组:
Array
(
[0] => Array
(
[UserInProject] => Array
(
[id] => 11
[project_id] => 3
[position_id] => 1
[user_id] => 15
)
[Project] => Array
(
[id] => 3
[short_name] => proj1
[full_name] => project 1
[start_date] => 2010-01-01
[end_date] => 2010-05-01
[agreement_number] => 12/34U/23
[active] => 1
[user_id] => 1
)
[Position] => Array
(
[id] => 1
[name] => some_name
)
[User] => Array
(
[id] => 15
[username] => foo
[first_name] =>
[last_name] =>
[email] => foo@foo.bar
[active] => 1
[created] =>
[modified] =>
)
[RegularPost] => Array
(
[0] => Array
(
[id] => 792
[date] => 2010-01-01
[size] => 0.20000
[users_in_project_id] => 11
)
[1] => Array
(
[id] => 793
[date] => 2010-02-01
[size] => 0.20000
[users_in_project_id] => 11
)
( and many more ...)
)
)
[1] => Array
(
[UserInProject] => Array
(
[id] => 20
[project_id] => 3
[position_id] => 2
[user_id] => 15
)
[Project] => Array
(
[id] => 3
[short_name] => proj1
[full_name] => project 1
[start_date] => 2010-01-01
[end_date] => 2010-05-01
[agreement_number] => 12/34U/23
[active] => 1
[user_id] => 1
)
[Position] => Array
(
[id] => 2
[name] => some_name2
)
[User] => Array
(
[id] => 15
[username] => foo
[first_name] =>
[last_name] =>
[email] => foo@foo.bar
[active] => 1
[created] =>
[modified] =>
)
[RegularPost] => Array
(
[0] => Array
(
[id] => 836
[date] => 2010-01-01
[size] => 0.2
[users_in_project_id] => 20
)
[1] => Array
(
[id] => 837
[date] => 2010-02-01
[size] => 0.3
[users_in_project_id] => 20
)
[2] => Array
(
[id] => 838
[date] => 2010-03-01
[size] => 0.3
[users_in_project_id] => 20
)
( and many more ...)
)
)
)
我想要实现的是上面的数组,但是带有[date] =>的RegularPost仅限2010-02-01。 有没有办法在此查询中将日期'2010-02-01'传递给RegularPost?
此查询不起作用:
$userInProjects = $this->Timesheet->RegularPost->UserInProject->find('all', array('conditions' => array('UserInProject.user_id' => $id, 'RegularPost.date' => '2010-02-01')));
1054:'where子句'
中的未知列'RegularPost.date'请帮忙。 :)
答案 0 :(得分:2)
您需要使用Containable行为:
$userInProjects = $this->Timesheet->RegularPost->UserInProject->find('all', array(
'conditions' => array('UserInProject.user_id' => $id),
'contain' => array(
'User', 'Position', 'Project',
'RegularPost' => array(
'conditions'=> array( 'RegularPost.date' => '2010-02-01' )
)
)
));
此查询告诉Cake的ORM使用您在UserInProject
中提供的id
属性以及所有关联的$id
,User
检索所有Position
条记录和Project
条记录。最后,它告诉Cake的ORM检索与RegularPost
属性等于UserInProject
的已检索date
记录关联的所有2010-02-01
条记录。
答案 1 :(得分:0)
@Rob Wilkerson
UserInProject hasMany RegularPost
和RegularPost belongsTo UserInProject
是的,我已经调试了2级调试。
以下是SQL查询:
SELECT `UserInProject`.`id`, `UserInProject`.`project_id`, `UserInProject`.`position_id`, `UserInProject`.`user_id`, `Project`.`id`, `Project`.`short_name`, `Project`.`full_name`, `Project`.`start_date`, `Project`.`end_date`, `Project`.`agreement_number`, `Project`.`active`, `Project`.`user_id`, `Position`.`id`, `Position`.`name`, `User`.`id`, `User`.`username`, `User`.`first_name`, `User`.`last_name`, `User`.`email`, `User`.`password`, `User`.`active`, `User`.`created`, `User`.`modified`, (CONCAT(`User`.`last_name`, " ", `User`.`first_name`, " (", `User`.`username` , ")")) AS `User__full_name` FROM `user_in_projects` AS `UserInProject` LEFT JOIN `projects` AS `Project` ON (`UserInProject`.`project_id` = `Project`.`id`) LEFT JOIN `positions` AS `Position` ON (`UserInProject`.`position_id` = `Position`.`id`) LEFT JOIN `users` AS `User` ON (`UserInProject`.`user_id` = `User`.`id`) WHERE `UserInProject`.`user_id` = 15
(此查询由cakephp自动生成):
SELECT `RegularPost`.`id`, `RegularPost`.`date`, `RegularPost`.`size`, `RegularPost`.`users_in_project_id` FROM `regular_posts` AS `RegularPost` WHERE `RegularPost`.`users_in_project_id` IN (11, 20)
那么,是否可以将日期'2010-02-01'传递给第二个查询?