get ids of all inserted records by last query executed in cakephp
I can find the primary key id of last inserted record as follows:
$this->Model->save($record);
$id = $this->Model->getLastInsertId();
I am looking for something like this:
$this->Model->saveAll($records);
$ids = $this->Model->getLastInsertIds();
I am inserting 100s of records, so its better to insert them all in a single query. Still its taking some seconds to execute the use case. Is there a way to get all ids of inserted recor开发者_运维技巧ds by last query?
This was asked in many forums, but there are no clear answer.
I think this might work and might be what you are looking for:
$this->Model->saveAll($records);
$id1 = $this->Model1->id();
$id2 = $this->Model2->id();
$id3 = $this->Model3->id();
...
Remember to replace model1,model2,model3 with each of the models that you are saving to.
Have you looked at the cakephp tutorial on saving your data? here is the link: http://book.cakephp.org/view/1031/Saving-Your-Data
Hope this helps.
精彩评论