New syntax for “order by” clauses with several columns

Published on and tagged with cakephp  feature

Up to now you defined an “order by” clause with several columns in the following way:

$this->User->findAll(null, null, 'User.lastname ASC, User.firstname ASC');

In the coming release of CakePHP that will no longer work, it causes an “Unknown column” error. You have to use a new syntax:

$this->User->findAll(null, null, 
                                    array('User.lastname' => 'ASC', 'User.firstname' => 'ASC'));

Update (2006-02-16): The statement above could be simplified in the following way:

$this->User->findAll(null, null, array('User.lastname', 'User.firstname'));

You only have to use the key/value syntax if you want a descending order:

$this->User->findAll(null, null, array('User.lastname', 'User.firstname' => 'DESC'));

Thanks to PhpNut for that hint.

2 comments baked

© daniel hofstetter. Licensed under a Creative Commons License