Cakephp 3.0 how to get lastInsertId


Dear Sir ,
i am new to cakephp 3.x so i just want to know how to get inserted id after data save in controller.I have used $this->MyModel->lastInsertId() but this shows error unknown method.please help


In Cake 3.x you get the Entity as a return value from the save operation:

$entity = $this->MyModel->newEntity($this->request->data())
$result= $this->MyModel->save($entity); //Statement as result
$id = $result->id;

This should work fine.


Thank you sir yest it works.