Mysql CakePhp连接三个表重复数组元素
我有三个表格作业、标签和作业标签。我将Job.php模型中的表联接如下:Mysql CakePhp连接三个表重复数组元素,mysql,cakephp,join,Mysql,Cakephp,Join,我有三个表格作业、标签和作业标签。我将Job.php模型中的表联接如下: $job_cond = array('Job.status = 1'); if($job_id != NULL){ $condition['Job.id'] = $job_id; array_push($job_cond,"Job.id = $job_id"); } $tag_cond = ""; if($tag
$job_cond = array('Job.status = 1');
if($job_id != NULL){
$condition['Job.id'] = $job_id;
array_push($job_cond,"Job.id = $job_id");
}
$tag_cond = "";
if($tag_id != NULL){
$tag_cond = array("Tag.id = $tag_id");
}
$options = array(
'fields' => array('Job.*', 'Tag.*'),
'conditions' => $job_cond,
'order' => array('Job.added_on DESC'),
'recursive' => -1,
'joins' =>
array(
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'inner',
'conditions' => $tag_cond,
),
array(
'table' => 'job_tags',
'alias' => 'JobTag',
'type' => 'inner',
'conditions' => array('JobTag.job_id = Job.id','JobTag.tag_id = tag.id'),
)
)
);
$res = $this->find('all', $options); echo count($res);
array(
(int) 0 => array(
'Job' => array(
'id' => '7',
'title' => 'th fgh fghfgh',
'description' => 'fgh fh ffgh',
'email' => 'dsaf@dfg.com',
'min_experience' => '2',
'max_experience' => '3',
'freshers_apply' => 'No',
'phone' => '56546546',
'address' => 'df',
'posted_on' => '2015-02-27',
'status' => '1',
'added_on' => '2015-02-27 16:57:05'
),
'Tag' => array(
'id' => '3',
'name' => 'Sales',
'status' => '1'
)
),
(int) 1 => array(
'Job' => array(
'id' => '5',
'title' => 'dfg dfgdfg ',
'description' => 'dfg dfgdfg',
'email' => 'sdfsdf@dfgfg.com',
'min_experience' => '2',
'max_experience' => '3',
'freshers_apply' => 'No',
'phone' => '345345345',
'address' => 'df',
'posted_on' => '2015-02-27',
'status' => '1',
'added_on' => '0000-00-00 00:00:00'
),
'Tag' => array(
'id' => '5',
'name' => 'Database',
'status' => '1'
)
),
(int) 2 => array(
'Job' => array(
'id' => '5',
'title' => 'dfg dfgdfg ',
'description' => 'dfg dfgdfg',
'email' => 'sdfsdf@dfgfg.com',
'min_experience' => '2',
'max_experience' => '3',
'freshers_apply' => 'No',
'phone' => '345345345',
'address' => 'df',
'posted_on' => '2015-02-27',
'status' => '1',
'added_on' => '0000-00-00 00:00:00'
),
'Tag' => array(
'id' => '1',
'name' => 'IT',
'status' => '1'
)
)
结果显示数组,但对于每个标记id,结果显示其包含的每个标记的作业。也就是说,数组如下所示:
$job_cond = array('Job.status = 1');
if($job_id != NULL){
$condition['Job.id'] = $job_id;
array_push($job_cond,"Job.id = $job_id");
}
$tag_cond = "";
if($tag_id != NULL){
$tag_cond = array("Tag.id = $tag_id");
}
$options = array(
'fields' => array('Job.*', 'Tag.*'),
'conditions' => $job_cond,
'order' => array('Job.added_on DESC'),
'recursive' => -1,
'joins' =>
array(
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'inner',
'conditions' => $tag_cond,
),
array(
'table' => 'job_tags',
'alias' => 'JobTag',
'type' => 'inner',
'conditions' => array('JobTag.job_id = Job.id','JobTag.tag_id = tag.id'),
)
)
);
$res = $this->find('all', $options); echo count($res);
array(
(int) 0 => array(
'Job' => array(
'id' => '7',
'title' => 'th fgh fghfgh',
'description' => 'fgh fh ffgh',
'email' => 'dsaf@dfg.com',
'min_experience' => '2',
'max_experience' => '3',
'freshers_apply' => 'No',
'phone' => '56546546',
'address' => 'df',
'posted_on' => '2015-02-27',
'status' => '1',
'added_on' => '2015-02-27 16:57:05'
),
'Tag' => array(
'id' => '3',
'name' => 'Sales',
'status' => '1'
)
),
(int) 1 => array(
'Job' => array(
'id' => '5',
'title' => 'dfg dfgdfg ',
'description' => 'dfg dfgdfg',
'email' => 'sdfsdf@dfgfg.com',
'min_experience' => '2',
'max_experience' => '3',
'freshers_apply' => 'No',
'phone' => '345345345',
'address' => 'df',
'posted_on' => '2015-02-27',
'status' => '1',
'added_on' => '0000-00-00 00:00:00'
),
'Tag' => array(
'id' => '5',
'name' => 'Database',
'status' => '1'
)
),
(int) 2 => array(
'Job' => array(
'id' => '5',
'title' => 'dfg dfgdfg ',
'description' => 'dfg dfgdfg',
'email' => 'sdfsdf@dfgfg.com',
'min_experience' => '2',
'max_experience' => '3',
'freshers_apply' => 'No',
'phone' => '345345345',
'address' => 'df',
'posted_on' => '2015-02-27',
'status' => '1',
'added_on' => '0000-00-00 00:00:00'
),
'Tag' => array(
'id' => '1',
'name' => 'IT',
'status' => '1'
)
)
因此,我想知道如何删除不同标记ID的重复作业如果一个具有1行的表连接到一个具有2行的表,即一个具有2个标记的作业,SQL将返回两行 尝试添加
'group' => 'Job.id'
到您的选项数组
请注意,如果在模型和可包含行为中正确设置了关联,则在查找中既不需要联接也不需要组