Ângelo Rigo
Ângelo Rigo

Reputation: 2157

Join with two filters in CakePHP

How can I write into CakePHP 2.5.2 a left join:

SELECT A.*, B.*, C.*
FROM A
JOIN B on A.id = B.a_id
JOIN c ON (c.b_id = b.id AND c.a_id = a.id)

How can this be written AND c.a_id=a.id using the find model method into the contain array?

Upvotes: 1

Views: 149

Answers (1)

Inigo Flores
Inigo Flores

Reputation: 4469

This should work:

$options['joins'] = array(
    array('table' => 'table_b',
        'alias' => 'B',
        'type' => 'LEFT',
        'conditions' => array(
            'A.id = B.a_id',
        )
    ),
    array('table' => 'table_c',
        'alias' => 'C',
        'type' => 'LEFT',
        'conditions' => array(
            "C.b_id = B.id",
            "C.a_id = A.id"
        )
    )
);

$ItemA->find('all', $options);

For further reference see:

Upvotes: 1

Related Questions