Hi Rob,

For complex conditions you're probably better off using a combination
of strings and arrays.

I find they're much simpler to read than cake's array syntax...

'conditions' => array(
        array('alert_subject_id' => $subject),
        '(Alert.start_date IS NULL OR Alert.start_date < NOW())',
        '(Alert.end_date IS NULL OR Alert.end_date > NOW())',
)


Also, the reason why your initial array wasn't working is because you
defined two "OR" keys in the same array, when only one can exist.

Cheers,
Adam

On Jan 8, 6:58 am, Rob Wilkerson <[email protected]> wrote:
> I'm working on what I guess I would consider the very low end of a
> complex find and yet I can't seem to get it quite right.
>
> In my Alert model, I'm trying to find all of the alerts of a
> particular type whose start date is null or less than NOW and whose
> end date is null or greater than NOW:
>
> return $this->find (
>         'list',
>         array (
>                 'fields' => array ( 'Alert.message' ),
>                 'conditions' => array (
>                         'Alert.alert_subject_id' => strtoupper ( $subject ),
>                         'OR' => array (
>                                 'Alert.start_date'     => null,
>                                 'Alert.start_date <= ' => date ( 'Y-m-d 
> H:i:s' )
>                         ),
>                         'OR' => array (
>                                 'Alert.end_date'       => null,
>                                 'Alert.end_date >= '   => date ( 'Y-m-d 
> H:i:s' )
>                         )
>                 )
>         )
> );
>
> The SQL I get out seems to ignore the start_date conditions, but
> includes the end_date conditions as I'd expect. If I switch the order,
> then the end_date conditions are ignored and the start_date conditions
> are translated to SQL as I'd expect. In my debug output, here's what I
> get for this particular find() syntax (formatted for some semblance of
> readability):
>
> SELECT
>    `Alert`.`id`,
>    `Alert`.`message`
> FROM `alerts` AS `Alert`
> WHERE
>    `Alert`.`alert_subject_id` = 'TICKET'
>    AND (
>       (`Alert`.`end_date` IS NULL) OR (`Alert`.`end_date` >=
> '2009-01-07 15:54:09')
>    )
>
> As you can see, no mention of the start_date conditions at all. Am I
> missing an array or something? Have I been looking at this too long to
> see something else that's absurdly obvious?  I'm using the final
> release of v1.2.
>
> Any additional eyeballs would be a much appreciated sanity check.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to