Skip to main content
 首页 » 编程设计

cakephp之Cakephp 中的 getLastInsertId() 等价于什么

2024年05月06日22kerrycode

如果我在 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