我有一个Job
模型和一个Visit
模型,一个Job可以有很多Visit。我已经在我的模型中适当地建立了关系。
访问也属于Supplier
,因此Supplier
可以有多次访问。
转包商是数据库中engineer
值为0的供应商。
我想返回一个关联数组,其中包含每个转包商募集的作业数量,如下所示:
$supplier_job_count = array(
'Subcontractor1' => 23,
'Subcontractor2' => 3,
'Subcontractor3' => 7,
'Subcontractor4'=> 0
);
这就是我所拥有的:
$conditions = array();
// get all the visits in the database
$visits = $this->Job->Visit->find('all');
// extract all the visit id's
$visit_supplier_ids = Hash::extract($visits, '{n}.Visit.supplier_id'); // 11612 records
// get all the subcontractors in the database
$subcontractors = $this->Job->Visit->Supplier->find('all', array('order' => 'id ASC', 'conditions' => array('Supplier.engineer' => 0)));
// extract all the subcontractor id's
$subcontractor_ids = Hash::extract($subcontractors, '{n}.Supplier.id'); // 1288 records
// intersect arrays so we only want matching values from both, i.e. only want visits whose supplier is a subcontractor
$visit_subcontractors = array_values(array_intersect($visit_supplier_ids, $subcontractor_ids));
// find all visits who is a subcontractor id
$visits = $this->Job->Visit->find('all', array('conditions' => array('Visit.supplier_id' => $visit_subcontractors)));
// extract out the job id's for the subcontractor visits
$visit_jobs_ids = Hash::extract($visits, '{n}.Visit.job_id');
// pass the job id's to the conditions for the jobs
$conditions['Job.id'] = $visit_jobs_ids;
// get all subcontractor jobs
$subcontractor_jobs = $this->Job->find('all', array('conditions' => $conditions));
$subcontractor_job_count = array();
foreach ($subcontractors as $key => $value) {
// this is where I am getting stuck
$subcontractor_job_count[$value['Supplier.name']] = ));
}
我在那里有一半,只是努力计算每个分包商提出的工作岗位数量,任何想法?