What is the equivalent to getLastInsertId() in Cakephp?

后端 未结 22 609
囚心锁ツ
囚心锁ツ 2020-11-28 11:00

If I do getLastInsertId() immediately after a save(), it works, but otherwise it does not. This is demonstrated in my controller:

f         


        
相关标签:
22条回答
  • 2020-11-28 11:34

    Try to use this code. try to set 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();
    
    0 讨论(0)
  • 2020-11-28 11:35

    There are several methods to get last inserted primary key id while using save method

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

    This will return last inserted id of the model current model

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

    This will return last inserted id of model with given model name

    $this->Model->id;
    

    This will return last inserted id of last loaded model

    $this->id;
    
    0 讨论(0)
  • 2020-11-28 11:37

    This is interesting, I also stumbled upon this issue. What you asked perhaps how to get the last ID of a certain model regardless of it's state, whether it's just been inserted or not. To further 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, that's the only piece of code inside that function. It means that cakephp caches any last inserted ID, instead of retrieve it from the database. That's why you get nothing if you use that function when you haven't done any record creation on the model.

    I made a small function to get the last ID of a certain table, but please note that this should not be used as a replacement of getLastID() or getLastInsertID(), since it has an entirely different purpose.

    Add the function lastID() to the AppModel as shown below so that it can be used system wide. It has it's limit, which can't be used on model with 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 : Class Model

    0 讨论(0)
  • 2020-11-28 11:39

    In CakePHP you can get it by: Model::getInsertID() //Returns the ID of the last record this model inserted. Model::getLastInsertID() //Alias to getInsertID().

    0 讨论(0)
  • 2020-11-28 11:40

    this is best way to find out last inserted id.

    $this->ModelName->getInsertID();
    

    other way is using

    $this->ModelName->find('first',array('order'=>'id DESC')) 
    
    0 讨论(0)
  • 2020-11-28 11:41

    This will return last inserted id of last loaded model

    $this->id;
    

    This will return last inserted id of model with given model name

    $this->Model->id;
    

    This will return last inserted id of the model current model

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

    echo $this->ModelName->getInsertID();
    echo $this->ModelName->getLastInsertID();
    
    0 讨论(0)
提交回复
热议问题