What is the equivalent to getLastInsertId() in Cakephp?

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

    I think it works with getLastInsertId() if you use InnoDB Tables in your MySQL Database. You also can use $this->Model->id

    0 讨论(0)
  • 2020-11-28 11:45
    $this->Model->field('id', null, 'id DESC')
    
    0 讨论(0)
  • 2020-11-28 11:46

    You'll need to do an insert (or update, I believe) in order for getLastInsertId() to return a value. Could you paste more code?

    If you're calling that function from another controller function, you might also be able to use $this->Form->id to get the value that you want.

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

    Use this one

    function designpage() {
    //to create a form Untitled
    $this->Form->saveField('name','Untitled Form');
    echo $this->Form->id; //here it works
    

    }

    0 讨论(0)
提交回复
热议问题