如何在cakephp中找到最新的记录?
16lz
2021-01-22
I've got a a table with a column request_time. There will be multiple requests and I want to get the latest request of a particular username:
我有一个包含request_time列的表。将有多个请求,我想获得特定用户名的最新请求:
$this->Requests->find('all', 'conditions' => array (
'username' => $username,
'MAX'=> 'request_time'));
The above doesn't work. Is there something in Cakephp or do I need to do my own ->query()?
以上不起作用。在Cakephp中有什么东西或者我需要自己做 - > query()吗?
2 个解决方案
#1
12
You can use the following:
您可以使用以下内容:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('id' => 'DESC') ));
Where id
is the auto incremented primary key. This will give you the latest (single) record, if you are using first
in find method, or else use all
instead.
其中id是自动递增的主键。如果您在find方法中首先使用,这将为您提供最新的(单个)记录,或者使用all。
If you are not using primary key, you can try the following:
如果您不使用主键,则可以尝试以下操作:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('request_time' => 'DESC') ));