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
Finally found the answer, if anybody else runs into this do:
$result=$this->ModelName->save($whatever); $record_id=$result->id;
to cakephp3.X I Found this:
if ($articlesTable->save($article)) { // The $article entity contains the id now $id = $article->id; }
http://book.cakephp.org/3.0/en/orm/saving-data.html#inserting-data
You can solve this issue following way.
For CakePHP 3.x
$result = $this->ModelName->save($this->request->data);
$insertedId = $result->id;
For CakePHP 2.x
$this->ModelName->save($this->request->data);
$insertedId = $this->ModelName->getLastInsertId();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With