What is equivalent to getLastInsertId () in CakePHP?

If I execute getLastInsertId() immediately after save() , it works, but otherwise it doesn’t. This is demonstrated in my controller:

 function designpage() { //to create a form Untitled $this->Form->saveField('name','Untitled Form'); echo $this->Form->getLastInsertId(); //here it works } function insertformname() { echo $this->Form->getLastInsertId(); //this doesnt echo at all } 

Please suggest a way to get the necessary functionality.

+47
cakephp
Jun 11 '09 at 6:58
source share
22 answers

CakePHP has two methods for getting the last inserted id: Model::getLastInsertID() and Model::getInsertID() . In fact, these methods are identical, so it really does not matter which method you use.

 echo $this->ModelName->getInsertID(); echo $this->ModelName->getLastInsertID(); 

These methods can be found in cake/libs/model/model.php on line 2768

+91
Apr 08 2018-11-11T00:
source share

Just use:

 $this->Model->id; 
+26
Sep 02 '10 at 14:20
source share

In Cake, the last insertion identifier is automatically stored in the id property of the model. Therefore, if you simply inserted the user through the User model, the last insert identifier can be obtained through $ User-> id

id - The value of the primary key identifier is the entry that this model is currently pointing to. Automatically after pasting into the database.

More information about model properties in CakePHP API Docs: http://api.cakephp.org/2.5/class-AppModel.html

Edit: I only realized that Model :: getLastInsertID () is essentially the same as Model-> id

Having looked at your code more closely, it’s hard to say exactly what you are doing with various functions and where they exist in the great scheme of things. It can be rather a volume problem. Are you trying to access the latest insert identifier in two different requests?

Can you explain the flow of your application and how it relates to your problem?

+18
Jun 11 '09 at 13:53
source share

You will need to do an insert (or update, I suppose) for getLastInsertId() return a value. Could you insert more code?

If you call this function from another controller function, you can also use $this->Form->id to get the desired value.

+9
Jun 11 '09 at 7:14
source share

this is the best way to find out the last inserted id.

 $this->ModelName->getInsertID(); 

another way uses

 $this->ModelName->find('first',array('order'=>'id DESC')) 
+8
Dec 13 '13 at 11:22
source share

There are several ways to get the last inserted primary key identifier using the save method.

 $this->loadModel('Model'); $this->Model->save($this->data); 

This will return the last inserted identifier of the current model model

 $this->Model->getLastInsertId(); $this->Model-> getInsertID(); 

This returns the last inserted model identifier with the given model name

 $this->Model->id; 

This will return the last inserted id of the last loaded model

 $this->id; 
+7
Jun 13 2018-12-17T00:
source share

Try using this code in your model class (possibly in the AppModel):

 function get_sql_insert_id() { $db =& ConnectionManager::getDataSource($this->useDbConfig); return $db->lastInsertId(); } 

Caveat emptor: MySql function LAST_INSERT_ID () only works with tables with the AUTO_INCREMENT field (otherwise it returns 0). If your primary key does not have an AUTO_INCREMENT attribute, this may be the cause of your problems.

+6
Sep 03 '10 at 0:02
source share

Try using this code. try setting it to a variable so you can use it in other functions. :)

 $variable = $this->ModelName->getLastInsertId(); 

in PHP native, try this.

 $variable = mysqli_insert_id(); 
+6
09 Oct '14 at 5:43
source share

This will return the last inserted id of the last loaded model

 $this->id; 

This returns the last inserted model identifier with the given model name

 $this->Model->id; 

This will return the last inserted identifier of the current model model

CakePHP has two methods for getting the last inserted id: Model::getLastInsertID() and Model::getInsertID() .

 echo $this->ModelName->getInsertID(); echo $this->ModelName->getLastInsertID(); 
+4
Mar 04 '14 at 6:17
source share

The following are the options:

 echo $this->Registration->id; echo $this->Registration->getInsertID(); echo $this->Registration->getLastInsertId(); 

Here you can replace Registration with your model name.

thank

+4
Jan 31 '16 at 14:27
source share

Use this

 function designpage() { //to create a form Untitled $this->Form->saveField('name','Untitled Form'); echo $this->Form->id; //here it works 

}

+3
May 23 '13 at 6:32
source share

You can get the last nested identifier in many ways. Similar to the name of the User model, the best way to get the last inserted identifier is

 $this->User->id; // For User Model 

You can also use the model function, but below the code will return the last inserted model identifier with the given model name for this example, it will return user model data

 $this->User->getLastInsertId(); $this->User->getInsertID(); 
+3
Sep 02 '13 at 13:48 on
source share

When you use save() , the last insert identifier is set to the $id model property. So:

 if ($this->Model->save()) { printf('Last insert ID was %s', $this->Model->id); } 
+3
Sep 27 '13 at 8:38
source share

Each time the save method is called on the model, the cake internally calls Model :: getLastInsertId () and stores the result in the identifier of the model class attribute, so after calling save () there is no need to call Model :: getLastInsertId () or inserId (), since this value can be obtained directly in this way

 $id = $this->id;// within a model $id = $this->{$this->modelName}->id;// in a controller 
+3
Oct 23 '13 at 19:18
source share

After entering the data, we can use the following code to get the newly added record identifier:

  $last_insert_id=$this->Model->id; 
+2
Jun 20 '13 at 18:21
source share

every time you perform an insert operation in any model, the cake inside the fetchesthe last insert attribute is Id and Sets to Model-> id.

to access it directly with $ Model-> id ;, you don’t need to request lastInsertId again.

+2
Sep 27 '13 at 8:35
source share

I think it works with getLastInsertId() if you use InnoDB tables in your MySQL database. You can also use $this->Model->id

+2
Jul 24 '14 at 0:03
source share
 $Machinedispatch = $this->Machinedispatch->find('first',array('order'=>array('Machinedispatch.id DESC'))); 

The easiest way to find the last inserted row. For me, getLastInsertId () does not work.

+1
Jul 15 2018-11-17T00:
source share

This is interesting, I also came across this problem. What you asked is possibly how to get the last identifier of a particular model, regardless of its state, whether it was inserted or not. To understand what getInsertID does, we need to take a look at the source:

Link 1: http://api20.cakephp.org/view_source/model#line-3375

 public function getInsertID() { return $this->_insertID } 

Yup, this is the only piece of code inside this function. This means cakephp caches any last inserted identifier, rather than retrieving it from the database. This is why you get nothing if you use this function when you have not done any record creation on the model.

I made a small function to get the last identifier of a specific table, but note that this should not be used as a replacement for getLastID() or getLastInsertID() , since it has a completely different purpose.

Add the lastID() function to the AppModel as shown below so that it can be used in the system area. It has a restriction that cannot be used for a model with a composite primary key.

 class AppModel extends Model { public function lastID() { $data = $this->find('first', array( 'order' => array($this->primaryKey . ' DESC'), 'fields' => array($this->primaryKey) ) ); return $data[$this->name][$this->primaryKey]; } } 

Original source: cool model

+1
Dec 23
source share

You are actually using getLastInsertId or getInsertId incorrectly. getLastInsertId () is intended to work only after the save () method. It will not even work after manual insertion, since the cake mechanism stores mysql_insert_id under $ this → _ insertID inside the save method, which can be obtained via getLastInsertId or getInsertId. Now in your case

 $this->Model->id 

OR

 $this->Model->find('first',array('order'=>'id DESC')) 

Will do.

+1
Jan 09 '14 at 18:55
source share

In CakePHP you can get it: Model :: getInsertID () // Returns the identifier of the last record inserted into this model. Model :: getLastInsertID () // Alias ​​getInsertID ().

+1
Mar 06 '14 at 23:41
source share
 $this->Model->field('id', null, 'id DESC') 
0
Aug 24 '10 at 13:47
source share



All Articles