• support@answerspoint.com

What is the equivalent to getLastInsertId() in Cakephp?


If I do getLastInsertId() immediately after a save(), it works, but otherwise it does not. 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 functionality I want.





CakePHP has two methods for getting the last inserted id: Model::getLastInsertID() and Model::getInsertID(). Actually these methods are identical so it really doesn't matter which method you use.

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

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

  • answered 7 years ago
  • B Butts

Your Answer

    Facebook Share        
  • asked 7 years ago
  • viewed 1840 times
  • active 7 years ago

Best Rated Questions