What is the equivalent to getLastInsertId() in Cakephp?

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

    Actually you are using the getLastInsertId or getInsertId in a wrong manner. getLastInsertId() is meant to work only after save() method. It will even not work after a manual insert, as cake engine is storing the mysql_insert_id under $this->_insertID inside the save method which can be retrieved via the getLastInsertId or getInsertId. Now in your case

    $this->Model->id
    

    OR

    $this->Model->find('first',array('order'=>'id DESC')) 
    

    Will do.

提交回复
热议问题