5

我想使用created表中的字段检索 cakephp 中的最后 3 个注册用户Users

在我的控制器中,我有:

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

上面的代码在运行时返回此错误:

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 

我必须怎么做才能解决错误?

4

6 回答 6

7

尝试这样的事情:

$this->set('users', $this->User->find('all', array(
    'limit' => 3,
    'order' => 'User.created DESC',
    'recursive' => 1,
)));
于 2012-06-30T12:53:56.760 回答
3

保持您的订单并限制条件数组之外,然后它将顺利运行。

使用这种格式:

array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'), //fields to GROUP BY
'limit' => n, //int
'page' => n, //int
'offset' => n, //int
'callbacks' => true //other possible values are false, 'before', 'after'
)
于 2014-12-29T06:52:20.900 回答
2

您也可以通过以下 sql 查询轻松选择您的数据:

$sql = "SELECT * FROM users where recursive =1 order by created desc limit 0,3";
$users = $this->users->query($sql);

$this->set(compact('users'));
于 2015-10-15T11:19:17.777 回答
1

请尝试如下代码:

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

CakePHP 的查找条件请参考此链接

于 2012-06-30T12:51:08.530 回答
1

当您首先限制数据时,您应该订购DESC or ASC. 另一种工作正常的简单方法

$this->set('datas',
    $this->Your_Model_Name->find('all',
        array (
            'conditions'    => array('Site_id' => $site_name),   //conditions here
            'fields'        => array('Date_time','Ac_1_Status','Tempin'),
            'order'         => array('id' => 'desc'),  // id desc or asc
            'limit'         => 15
        )
    )
);
于 2017-06-07T07:07:54.613 回答
0
$dashreport =  $this->Task->find('all', array('conditions' => 
                array('Task.throttle' => "Report", 'Task.status' => Null, 'Task.userid' => $this->Session->read('userid')),'order'=>array('Task.id DESC')
                ,'limit'=> 4));
于 2016-07-12T08:41:18.203 回答