Reputation: 576
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
Upvotes: 23
Views: 37080
Reputation: 404
To get the identifier of the newly created registration cakePHP 3.x, it's very simple, just check if the entity was saved well, otherwise you will not have access to the identifier. The code is very simple
$this->loadModel('ModelName');
$newEntity = $this->ModelName->newEntity();
So after loading your model and declaring a new record, you can put the values on the entity before saving it. For example if it's a post request, you can do:
To proceed fields by fields:
$newEntity->name = $this->request->getData('name');
or (according to cakePHP version, first example is from the recent version)
$newEntity->name = $this->request->data('name');
Otherwise globally, in this case the name attributes of your fields in the form must be identical to the attributes of your entity in database.
$newEntity = $this->ModalName->patchEntity($newEntity, $this->request->getData());
And once the entity is ready, you will only have access to its identifier if it is registered in the database.
Solution to get the ID of the last record, you have to do this:
if($this->ModalName->save($newEntity) {
//you can get ID here
$id = $newEntity->id;
}
I hope it will be useful.
Upvotes: 2
Reputation: 504
This is the only way I was able to get this to work correctly:
$result=$this->ModelName->save($whatever);
$record_id=$result['id'];
Upvotes: -1
Reputation: 4765
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();
Upvotes: 7
Reputation: 675
I've been struggling with this for quite a while before getting it to work. The save method will update the entity with the last insert id as long as the entity id is not already set. If you are creating a new record and the entity id is set to zero the save method will not update it so be sure that the entity id is not set:
$table = TableRegistry::get('MyModel');
$record = $table->newEntity($data);
// Unset the id if present and set to zero:
if (isset($record->id) && ($record->id==0)){
unset($record->id);
}
if ($saved = $table->save($record)){
$id = $record->id; // get the last insert id
}
Upvotes: 0
Reputation: 1
This should work fine:
$entity = $this->MyModel->newEntity($this->request->data())
$result= $this->MyModel->save($entity);
$id = $result->id;
Upvotes: -1
Reputation: 112
In CakePHP 3.0
$statement = $query1->insert("columns of milestone");
$id = $statement->lastInsertId('Milestones'); //Here it will return the last insert id
NOTE Milestones is the name of table
Upvotes: 0
Reputation: 141
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
Upvotes: 14
Reputation: 99
These work for the actually ORM model.
$result = $this->Model->save($data);
$id = $result->id;
Upvotes: 1
Reputation: 245
$result=$this->ModelName->find('all',['fields'='id'])->last();
$record_id=$result->id;
Upvotes: 1
Reputation: 576
Finally found the answer, if anybody else runs into this do:
$result=$this->ModelName->save($whatever);
$record_id=$result->id;
Upvotes: 33