How do you get the last insert ID in CakePHP 3.0?

ben921 picture ben921 · Sep 24, 2014 · Viewed 32.6k times · Source

Working with the CakePHP 3.0 beta, seems like a simple problem, but I've searched through the docs and can't find anything. After inserting a new record using $this->Model->save(), I'd like to the get the auto_increment primary key ID of the newly created record.

With Cake 2.x, I could do:

$record_id=$this->ModelName->id;

or

$record_id=$this->ModelName->getLastInsertID();

However neither of those seems to work in CakePHP 3.0.

Thanks

Answer

ben921 picture ben921 · Sep 25, 2014

Finally found the answer, if anybody else runs into this do:

$result=$this->ModelName->save($whatever);
$record_id=$result->id;