本文介绍了如何获得CakePHP 3.0中的最后一个插入ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用CakePHP 3.0 Beta似乎是一个简单的问题,但是我搜索了所有文档,但找不到任何东西。使用$ this-> Model-> save()插入新记录后,我想获取新创建记录的auto_increment主键ID。
Working with the CakePHP 3.0 beta, seems like a simple problem, but I've searched through the docs and can't find anything. After inserting a new record using $this->Model->save(), I'd like to the get the auto_increment primary key ID of the newly created record.
使用Cake 2.x,我可以执行以下操作:
With Cake 2.x, I could do:
$record_id=$this->ModelName->id;
或
$record_id=$this->ModelName->getLastInsertID();
但是,这两个似乎都不在CakePHP 3.0中起作用。
However neither of those seems to work in CakePHP 3.0.
谢谢
推荐答案
最后找到答案了,如果有人遇到这个问题:
Finally found the answer, if anybody else runs into this do:
$result=$this->ModelName->save($whatever);
$record_id=$result->id;
这篇关于如何获得CakePHP 3.0中的最后一个插入ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!