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?

Milina Udara
  • 597
  • 1
  • 9
  • 24

3 Answers3

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!!!!

gdm
  • 7,647
  • 3
  • 41
  • 71
0

Use below code for cakePHP 3.0 or above

 $result = $this->ModelName->save($data)
 echo $result->id;
vinod
  • 2,850
  • 1
  • 18
  • 23
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.

olaurendeau
  • 659
  • 4
  • 12