This is interesting, I also came across this problem. What you asked is possibly how to get the last identifier of a particular model, regardless of its state, whether it was inserted or not. To understand what getInsertID does, we need to take a look at the source:
Link 1: http://api20.cakephp.org/view_source/model#line-3375
public function getInsertID() { return $this->_insertID }
Yup, this is the only piece of code inside this function. This means cakephp caches any last inserted identifier, rather than retrieving it from the database. This is why you get nothing if you use this function when you have not done any record creation on the model.
I made a small function to get the last identifier of a specific table, but note that this should not be used as a replacement for getLastID() or getLastInsertID() , since it has a completely different purpose.
Add the lastID() function to the AppModel as shown below so that it can be used in the system area. It has a restriction that cannot be used for a model with a composite primary key.
class AppModel extends Model { public function lastID() { $data = $this->find('first', array( 'order' => array($this->primaryKey . ' DESC'), 'fields' => array($this->primaryKey) ) ); return $data[$this->name][$this->primaryKey]; } }
Original source: cool model
Hendra Uzia Dec 23 2018-12-12T00: 00Z
source share