Cakephp 3.0 how to get lastInsertId


#1

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


#2

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.


#3

Thank you sir yest it works.