What is the equivalent to getLastInsertId() in Cakephp?

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

    In Cake, the last insert id is automatically saved in the id property of the model. So if you just inserted a user via the User model, the last insert id could be accessed via $User->id

    id - Value of the primary key ID of the record that this model is currently pointing to. Automatically set after database insertions.

    Read more about model properties in the CakePHP API Docs: http://api.cakephp.org/2.5/class-AppModel.html

    Edit: I just realized that Model::getLastInsertID() is essentially the same thing as Model->id

    After looking at your code more closely, it's hard to tell exactly what you're doing with the different functions and where they exist in the grand scheme of things. This may actually be more of a scope issue. Are you trying to access the last insert id in two different requests?

    Can you explain the flow of your application and how it relates to your problem?

提交回复
热议问题