How to find the last entry in cakephp?

I have a table with a request_time column. There will be several requests, and I want to get the last request for a specific username:

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

The above does not work. Is there something in Cakephp or do I need to make my own -> query ()?

+6
source share
2 answers

You can use the following:

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

Where id is the primary key with automatic addition. This will give you the most recent (single) entry if you use the first method in the search, or use all instead.

If you are not using a primary key, you can try the following:

 $this->Requests->find('first', array('conditions' => array('username' => $username), 'order' => array('request_time' => 'DESC') )); 
+12
source

If you have an automatically incremented primary key, you will find the last entry in this table, try

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

Source: https://habr.com/ru/post/948644/


All Articles