6

我有一张带有request_time 列的表。会有多个请求,我想获取特定用户名的最新请求:

$this->Requests->find('all', 'conditions' => array (
                                               'username' => $username, 
                                               'MAX'=> 'request_time'));

以上不起作用。Cakephp 中有什么东西还是我需要自己做->query()?

4

2 回答 2

14

您可以使用以下内容:

$this->Requests->find('first', array('conditions' => array('username' => $username), 
                               'order' => array('id' => 'DESC') ));

id自动递增的主键在哪里。first如果您正在使用find 方法,这将为您提供最新的(单个)记录,否则请使用all

如果您没有使用主键,您可以尝试以下操作:

$this->Requests->find('first', array('conditions' => array('username' => $username), 
                               'order' => array('request_time' => 'DESC') ));
于 2013-07-04T03:45:23.160 回答
3

如果您有自动递增的主键,那么您会找到该表的最新记录,试试这个

$this->Requests->find('find', 'conditions' => array ('username' => $username), 
                              'order' => array('id' => 'DESC') 
                      );
于 2013-07-03T17:29:36.323 回答