Event 模型有以下关系:
var $belongsTo = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id',
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
这是我根据用户输入列出事件的方式:
$conditions = array('user_id'=>$id, 'date >=' => $from, 'date <=' => $to);
$events = $this->find('all', array(
'conditions'=>$conditions, 'order' => array('Event.date' => 'asc')));
以下是正在运行的 3 个查询:
1 SELECT `User`.`id`, `User`.`name`, `User`.`surname` FROM `scheduling`.`users` AS `User` WHERE `company_id` = 1
2 SELECT `Project`.`id`, `Project`.`name` FROM `scheduling`.`projects` AS `Project` LEFT JOIN `scheduling`.`customers` AS `Customer` ON (`Project`.`customer_id` = `Customer`.`id`) WHERE `Project`.`company_id` = 1
3 SELECT `Event`.`id`, `Event`.`project_id`, `Event`.`user_id`, `Event`.`date`, `Event`.`hours`, `Event`.`minutes`, `Event`.`xhours`, `Event`.`xminutes`, `Event`.`xdetails`, `Event`.`assignment`, `Event`.`start_time`, `Event`.`material`, `Event`.`meter_drive`, `Event`.`time_drive`, `Event`.`start_location`, `Event`.`finish_time`, `Project`.`id`, `Project`.`name`, `Project`.`customer_id`, `Project`.`project_nr`, `Project`.`address`, `Project`.`post_nr`, `Project`.`city`, `Project`.`company_id`, `Project`.`color`, `Project`.`start_date`, `Project`.`finish_date`, `User`.`id`, `User`.`employee_nr`, `User`.`name`, `User`.`surname`, `User`.`email`, `User`.`password`, `User`.`role`, `User`.`phone`, `User`.`address`, `User`.`post_nr`, `User`.`city`, `User`.`token_hash`, `User`.`company_id`, `User`.`car_id`, `User`.`image` FROM `scheduling`.`events` AS `Event` LEFT JOIN `scheduling`.`projects` AS `Project` ON (`Event`.`project_id` = `Project`.`id`) LEFT JOIN `scheduling`.`users` AS `User` ON (`Event`.`user_id` = `User`.`id`) WHERE `user_id` = 1 AND `project_id` = 5 AND `date` >= '2013-07-01' AND `date` <= '2013-12-06' ORDER BY `Event`.`date` asc
事实上,我只需要第三个查询而不需要前两个。是什么导致它们以及如何摆脱它们?