What is the equivalent to getLastInsertId() in Cakephp?

后端 未结 22 733
囚心锁ツ
囚心锁ツ 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:24

    Each time a save method is called on a model, cake internally calls Model::getLastInsertId() and stores the result into model class attribute id, so after calling save() it is not necessary to call Model::getLastInsertId() or inserId(), as tha value can be directly accessed like this

    $id = $this->id;// within a model
    $id = $this->{$this->modelName}->id;// in a controller
    

提交回复
热议问题