我有几个表具有以下关系:
公司有很多工作,员工,卡车,用户
不
为单个公司分配并落在某一天(不使用可包含行为返回用户):
$this->set('resources', $this->Job->Company->find('first', array(
'conditions' => array(
'Company.id' => $company_id
),
'contain' => array(
'Employee',
'Truck',
'Job' => array(
'conditions' => array(
'Job.assigned' => false,
'Job.pickup_date' => date('Y-m-d', strtotime('Today'));
)
)
)
)));
现在,自从写了这段代码,我决定做更多的工作分配。所以我创建了一个新的模型“任务”,属于卡车和工作。我在卡车模型和作业模型中都添加了hasMany赋值。我在赋值表中有两个外键,还有一些其他赋值字段。
现在,我正在尝试获取上面相同的信息,只是不检查job表中的assigned字段,而是检查assignments表以确保job不存在。如果由于mysql错误而要使用find方法的“joins”特性,我就不能再使用containable行为(根据食谱)。但是,下面的查询返回所有作业,即使它们在不同的日期。
$this->set('resources', $this->Job->Company->find('first', array(
'joins' => array(
array(
'table' => 'employees',
'alias' => 'Employee',
'type' => 'LEFT',
'conditions' => array(
'Company.id = Employee.company_id'
)
),
array(
'table' => 'trucks',
'alias' => 'Truck',
'type' => 'LEFT',
'conditions' => array(
'Company.id = Truck.company_id'
)
),
array(
'table' => 'jobs',
'alias' => 'Job',
'type' => 'LEFT',
'conditions' => array(
'Company.id = Job.company_id'
)
),
array(
'table' => 'assignments',
'alias' => 'Assignment',
'type' => 'LEFT',
'conditions' => array(
'Job.id = Assignment.job_id'
)
)
),
'conditions' => array(
'Job.pickup_date' => $day,
'Company.id' => $company_id,
'Assignment.job_id IS NULL'
)
)));
编辑:事实证明,我的查询工作正常,我的数据出现问题,导致返回所有分配。