Search code examples
mysqlcakephpmysql-error-1064

cakephp query with conditions limit and order


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?


Solution

  • Try something like this:

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