What is the equivalent to getLastInsertId() in Cakephp?

后端 未结 22 730
囚心锁ツ
囚心锁ツ 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: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();
    

提交回复
热议问题