I'm trying to get my head around the complex find conditions of CakePHP and have read the docs but am struggling with this one query.
SELECT field1,
       field2
WHERE id = 123456
  AND ((holding_date = Last_day(holding_date)
        AND Month(holding_date) IN(3, 6, 9, 12))
       OR (holding_date = '2013-09-15'))
To produce the above conditions what would my conditions array look like?
While the conditions in the question are not that complex, they touch on a few points which mean they can be tricky to define correctly. Some of the things to know when defining cakephp conditions:
"AND" => ... in conditionsBearing in mind the above notes, the conditions in the question can be expressed as:
$foo->find('all', array(
    'fields' => array(
        'field1',
        'field2'
    ),
    'conditions' => array(
        'id' => 123456,
        'OR' => array(
            array(
                'holding_date = LAST_DAY(holding_date)',
                'MONTH(holding_date)' => array(3,6,9,12)
            ),
            'holding_date' => '2013-09-15'
        )
    )
));
Which results in:
WHERE 
    `id` = 123456
    AND 
    (
        (
            (holding_date = LAST_DAY(holding_date))  
            AND
            (MONTH(holding_date) IN (3, 6, 9, 12)))
        )
        OR 
        (`holding_date` = '2013-09-15')
    )
Note: whitespace is quite important =) I misread the question originally solely because of the inconsistent whitespace in the question's sql.
OK I have solved it:
$findParams['conditions'] = array(
            'Account.client_id' => '12345',
            'AND' => array(
                'OR' => array(
                    'Holding.holding_date' => '2013-09-15',
                    'AND' => array(
                        'Holding.holding_date = LAST_DAY(Holding.holding_date)',
                        'MONTH(Holding.holding_date)' => array(3,6,9,12)
                        )
                    )
                )
            );
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With