Multiple conditions 'AND' -not working

730 views
Skip to first unread message

Yamy

unread,
Jun 4, 2008, 2:54:50 PM6/4/08
to CakePHP Bleeding Edge
This may not be that 'Bleeding Edge' to most you Cake bakers; It's
probably just my inexperienced eye.. please take a look:

If "By default, CakePHP joins multiple conditions with boolean AND;"
Then my following snippet of code should work, showing both candidates
with 'next_contact_date' after one week ago, and before one week from
now:
_____________________________________________________________________________
function nextScheduled() {
$this->Candidate->recursive = 0;
$this->set('candidates', $this->paginate());

$next_scheduled = $this->Candidate->find('all',
array(
'conditions' => array(
<b>'Candidate.next_contact_date' => ">= " . date('Y-m=d' ,
strtotime("-1 weeks")),</b>
<b>'Candidate.next_contact_date' => "<= " . date('Y-m=d' ,
strtotime("+1 weeks")),</b>
),
'order' => 'next_contact_date ASC',
'recursive' => 0
)
);
$this->set('next_scheduled', $next_scheduled);
}
______________________________________________________________________________
Either condition:
<b>'Candidate.next_contact_date' => ">= " . date('Y-m=d' ,
strtotime("-1 weeks")),</b>
<b>'Candidate.next_contact_date' => "<= " . date('Y-m=d' ,
strtotime("+1 weeks")),</b>
Used by itself works fine, but together, only the later (<= +1 weeks)
condition is applyed and it seems to ignore the first condition...

The idea was to show candidates that need to be contacted today, but
also show candidates from the previous and next week... maybe I'm
taking the wrong approach to this function? Can anyone help a me?

francky06l

unread,
Jun 4, 2008, 6:35:04 PM6/4/08
to CakePHP Bleeding Edge
It's quite normal since you override your first condition by the
second (ie: same key in your array).
Either you code it this way :
'conditions' => array('Candidate.next_contact_date >= " . date('Y-
m=d', strtotime("-1 weeks")),
'Candidate.next_contact_date <= " . date('Y-
m=d' ,strtotime("+1 weeks")));

or
'conditions' => array('and' =>
array(array('Candidate.next_contact_date' => ">= " . date('Y-m=d' ,
strtotime("-1 weeks"))),
array('Candidate.next_contact_date' => "<= " .
date('Y-m=d' ,
strtotime("+1 weeks"))));

PS: you can also use "between".

hth
Reply all
Reply to author
Forward
0 new messages