gazareth
gazareth

Reputation: 1154

CakePHP find conditions with multiple 'OR'

I'm having a real headache getting this to work correctly. This is my desired outcome in raw MySQL:

WHERE `EmailTemplate`.`is_archived` = '0' 
AND
((`EmailTemplate`.`project_id` IS NULL) OR (`EmailTemplate`.`project_id` = 101))
AND
((`EmailTemplate`.`user_id` IS NULL) OR (`EmailTemplate`.`user_id` = 44))

How does this translate into a CakePHP conditions array?

Upvotes: 1

Views: 1669

Answers (1)

gazareth
gazareth

Reputation: 1154

Solved this, eventually.

    $templateConditions = array(
            'EmailTemplate.is_archived' => 0,
            'AND' => array(
                       'OR' => array(
                                array('EmailTemplate.project_id IS NULL'),
                                array('EmailTemplate.project_id' => $this->request->data['Project']['project_id'])
                               )    
                     ),
                     array(
                       'OR' => array(
                                array('EmailTemplate.user_id IS NULL'),
                                array('EmailTemplate.user_id' => $this->Auth->user('id'))
                               )
                     )
            );

Upvotes: 2

Related Questions