cakephp query with conditions limit and order

user1427811 picture user1427811 · Jun 30, 2012 · Viewed 22.1k times · Source

I want to retrieve the last 3 registered users in cakephp using the field created in table Users.

In my controller I have:

$this->User->recursive = 1;
    $this->set('users',
        $this->User->find('all',
             array('conditions'=> array(
                  'limit' => 3,
                  'order' => array(
                  'created' => 'asc'
                  )
             )
         )
    )
);

The code above when run returns this error:

Syntax error or access violation: 1064 You have an error in your SQL 
syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near 'order = ('asc')' at line 

What must I do to resolve the error?

Answer

ifunk picture ifunk · Jun 30, 2012

Try something like this:

$this->set('users', $this->User->find('all', array(
    'limit' => 3,
    'order' => 'User.created DESC',
    'recursive' => 1,
)));