I have already written about defining conditions in RC6, but unfortunately, most of it is no longer valid ;-) So, in this post I will show the current way of defining conditions.
First some examples using the array syntax:
$this->User->findAll(array('User.firstname' => '= Daniel'));
$this->User->findAll(array('User.firstname' => 'LIKE %an%'));
$this->User->findAll(array('User.age' => '> 18'));
When using several conditions in the array, they are combined with an “AND”.
If you do not want for some reason that CakePHP does quote a value automatically, you can use “-!” as shown in the next example. I think you do not have to use “-!” very often, at least I do not see any use cases for it ;-)
$this->User->findAll(array('User.firstname' => "-! 'Daniel'"));
The most flexible way is to use a simple string with your condition(s). In this case you have to single quote the values, as shown in the example:
$this->User->findAll("User.firstname = 'Daniel' OR User.firstname = 'Hugo'");
3 Comments
A couple technical details:
When using ‘=’ as a comparison operator, you actually don’t need to include it, as that is the default, so:
$this->User->findAll(array(‘User.firstname’ => ‘= Daniel’));
becomes
$this->User->findAll(array(‘User.firstname’ => ‘Daniel’));
Also, you can now use nested arrays for complex conditions, so:
$this->User->findAll(“User.firstname = ‘Daniel’ OR User.firstname = ‘Hugo’”);
could be
$this->User->findAll(array(‘or’ => array(“User.firstname” => ‘Daniel’), array(‘User.firstname = ‘Hugo’”)));
Ok now that is more like it. I have been waiting for nesting! Hey Daniel, thanks for this site it is a great little house o’ cake!
Sam D
@Nate: Thanks for the further explanations.
@Sam D: Nice to hear you like it. Thanks :)