如果我在 save()
之后立即执行 getLastInsertId()
,它会起作用,但否则不会。这在我的 Controller 中得到了演示:
function designpage() {
//to create a form Untitled
$this->Form->saveField('name','Untitled Form');
echo $this->Form->getLastInsertId(); //here it works
}
function insertformname() {
echo $this->Form->getLastInsertId(); //this doesnt echo at all
}
请建议一种获得我想要的功能的方法。
请您参考如下方法:
CakePHP 有两种方法来获取最后插入的 id:Model::getLastInsertID()
和Model::getInsertID()
。 实际上这些方法是相同的,所以使用哪种方法并不重要。
echo $this->ModelName->getInsertID();
echo $this->ModelName->getLastInsertID();
此方法可以在cake/libs/model/model.php
中找到在线 2768