CakePhp连接三个表重复数组元素

时间:2015-03-03 15:17:22

标签: mysql cakephp join

我有三个表jobstagsjob_tags。我加入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_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);

结果显示数组,但对于每个tag_id,结果显示其包含的每个tag的作业。也就是说,数组如下:

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 个答案:

答案 0 :(得分:0)

如果一行有1行的表连接到有2行的表(即1个作业带2个标记),SQL将返回两行。

尝试添加

'group' => 'Job.id'

到您的选项数组。

(请注意,如果在模型中正确设置了关联并且可以包含行为,则在查找中既不需要连接也不需要组。)