How to get the last id in cakephp

I am using request in cakephp

$this->Menu->query("INSERT INTO frd_menus SET menuName= '".$_REQUEST['menuname']."'"); 

How to get the last insert id of this request in cakephp? Please advice.

+7
cakephp
source share
11 answers

Unless there is a specific reason.

You have to use

 $this->Menu->save($data) 

to insert data. Then you can use

 $this->Menu->getLastInsertId(); 

to get the last inserted id.

+18
source share

In most cases, you should use the model method to query your database.

Model::query() exists to execute complex queries that will be more complex than using model methods, or in some cases when you have no choice.

In your case, this is very useful, because Model::save() launches a callback in a framework that sets the Model::id attribute for you.

So, this allows you to return it immediately:

 if ($this->Menu->save($data)) { $lastId = $this->Menu->id; } 

A WARNING!

On the other hand, and as said by other answers, there is the following method:

 Model::getLastInsertId() 

But YOU SHOULD BE VERY CAREFUL with this method and YOU SHOULD NOT USE THIS IN THIS USE because it inserted LAST , but NOT the last one you just inserted in the previous code statement!

If the user (during another parallel request on the server) saves the data between save() and getLastInsertId() , he will return this second user!

THUS, the following BAD answers respond for this use case :

  • stack overflow
  • stack overflow
  • stack overflow
  • stack overflow
+10
source share
 $this->Menu->getLastInsertId(); 

you can get the last insert id using this

+4
source share

use the orderby desc option in cakephp.

 $lastCreated = $this->Menu->find('first', array('order' => array('Menu.filedname' =>'desc'))`); 
+4
source share

You can try:

 $data = array('menuName' => $_REQUEST['menuname']) $this->Menu->create(); $this->Menu->save($data); $id = $this->Menu->getLastInsertId(); 
+3
source share

Very easy. Just do it.

 $this->Menu->save($newMenu); $menuId = $this->Menu->id; 
+2
source share

try it

 $this->ModelName->save($this->request->data); $insertedId = $this->ModelName->id; 
+2
source share
 $this->Menu->query("INSERT INTO frd_menus SET menuName= '".$_REQUEST['menuname']."'"); $result = $this->Menu->query("SELECT LAST_INSERT_ID()"); $last_id = $result[0][0]; 

this should work, but I think it would be better if you create a connection between menus and frd_menus and use the save function

0
source share

In CakePHP 3.x you can do it like:

 $data = $this->model->save($data); $data->id; // this will give the inserted id 
0
source share
 $this->Menu->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 model model data

 $this->Menu->getLastInsertId(); $this->Menu->getInsertID(); 
-one
source share

The following are the options:

 echo $this->Registration->id; 

OR

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

OR

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

Here you can replace the registration with your model name.

Note. This function does not work if you run the insert request manually

thanks

-one
source share

All Articles