0

Is getLastInsertID and Model->id same? And which one can happen concurrency problem ?

$this->Model->save($this->data);
__thisFunctionTakesAVeryLongTimeToExecute(); //function 1
$insertId = $this->Model->getLastInsertId();

Does getLastInsertId() return the ID from the data I've saved 2 lines above. Or does it return the latest ID that's created?

I mean what happen if when the function 1 (__thisFunctionTakesAVeryLongTimeToExecute();) execute another user do an another save. then which id will i get?

3 Answers 3

0
 $this->Model->id

Is used to set an id and read or modify data related.

$this->Model->getLastInsertID();

Return the id of the last inserted row in this model.

For your last question, make a test ! And publish your answer here.

0

I have another similar problem. If I put

 $oid  = $this->Home->Order->getLastInsertID();
 $order = $this->Home->Order->find('first',array('conditions'=>array( 'Order.id'=>$oid)));

the model associations between Order and its hasMany tables are destroyed.

If I put

 $oid  = 1; // for example
 $order = $this->Home->Order->find('first',array('conditions'=>array( 'Order.id'=>$oid)));

the model associations are maintained!!!!

0

Use below code for cakePHP 3.0 or above

 $result = $this->ModelName->save($data)
 echo $result->id;

Not the answer you're looking for? Browse other questions tagged or ask your own question.