Nitish
Nitish

Reputation: 2763

CakePhp Join three tables repeats array element

I have three tables jobs,tags and job_tags. I join the tables in Job.php model as below :

$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);

The result displays the array but for each tag_id , the result displays job for each tag it contains. That is, the array is something as below :

array(
    (int) 0 => array(
        'Job' => array(
            'id' => '7',
            'title' => 'th fgh fghfgh',
            'description' => 'fgh fh ffgh',
            'email' => '[email protected]',
            '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' => '[email protected]',
        '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' => '[email protected]',
            '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'
        )
    )

So I want to know how do I remove the duplicate job for different tag ids

Upvotes: 0

Views: 60

Answers (1)

Jeff N
Jeff N

Reputation: 146

If a table with 1 row is joined to a table with 2 rows (i.e., 1 job with 2 tags), SQL will return two rows.

Try adding

'group' => 'Job.id'

to your options array.

(Note that with associations set correctly in the models and containable behavior you would require neither the joins nor the group in your find.)

Upvotes: 0

Related Questions