New syntax for “order by” clauses with several columns

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.



  1. Nate
    Posted February 16, 2006 at 3:22 pm | Permalink

    Happy now? ;-P

  2. Posted February 16, 2006 at 4:14 pm | Permalink

    Yeah, I like the new syntax :)

Post a Comment

Required fields are marked *

%d bloggers like this: