New syntax for conditions II

Published on and tagged with cakephp  feature  programming

Yesterday, I wrote already about the new syntax for conditions. Well, most of it is outdated today ;-) The examples I showed do no longer work, they cause an “sql syntax” error:

$this->User->findAll(array('User.firstname' => '= Daniel'));
$this->User->findAll(array('User.firstname' => 'LIKE %an%'));
$this->User->findAll(array('User.age' => '> 18'));

To make them work again, we have to add a space to the values:

$this->User->findAll(array('User.firstname' => ' = Daniel'));
$this->User->findAll(array('User.firstname' => ' LIKE %an%'));
$this->User->findAll(array('User.age' => ' > 18'));

There are also some new features. By default, all conditions are concatenated with “AND”. If you want to use an “OR”, you have to do it in the following way:

$this->User->findAll(array('User.firstname' => ' = Daniel', 
                                               ' OR User.firstname' => ' = Hugo'));

It is now also possible to use “–return” for a value, i.e. your value will not be quoted by CakePHP. Example:

$this->User->findAll(array('User.firstname' => "--return = 'Daniel'"));

Update (2006-02-28): The examples in this post are no longer valid!

4 comments baked

  • cake baker » New syntax for conditions

    […] Update (2006-02-21): The examples about specifying the conditions are no longer valid, see New syntax for conditions II. […]

  • ChrisC

    is there a rational for requiring the space by the developer? It seems to me that the system ought to be smart enough to check that and insert the space if it is lacking, thus saving the developer YA obscure syntactic error.

  • ChrisC

    I should mention that the space notwithstanding, I really like this new syntax. Good stuff.

  • Larry E. Masters aka PhpNut


    The reason the space is need is not that I can not add it if it is missing, it acts more like a terminator so I do not have to write a regex for every possible condition operator. This could be changing soon, most of the code I have added will be refactored before rolling into the releases.

    Doing it more of a POC

© daniel hofstetter. Licensed under a Creative Commons License