Cakephp中的getLastInsertId()相当于什么?

时间:2009-06-11 06:58:41

标签: cakephp

如果我在getLastInsertId()之后立即执行save(),它会起作用,但不会。这在我的控制器中得到了证明:

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
}

请建议一种获得我想要的功能的方法。

22 个答案:

答案 0 :(得分:95)

CakePHP有两种获取最后插入ID的方法:Model::getLastInsertID()Model::getInsertID()。 实际上这些方法是相同的,所以使用哪种方法并不重要。

echo $this->ModelName->getInsertID();
echo $this->ModelName->getLastInsertID();

此方法可在第2768行的cake/libs/model/model.php

中找到

答案 1 :(得分:28)

只需使用:

$this->Model->id;

答案 2 :(得分:20)

在Cake中,最后一个插入ID会自动保存在模型的id属性中。因此,如果您只是通过用户模型插入用户,则可以通过$ User-> id

访问最后一个插入ID
  

id - 主键ID的值   这个模型的记录   目前指向。自动   在数据库插入后设置。

在CakePHP API文档中详细了解模型属性:http://api.cakephp.org/2.5/class-AppModel.html

编辑:我刚才意识到Model :: getLastInsertID()与Model-> id

基本相同

在更仔细地查看您的代码之后,很难确切地说出您正在使用不同的功能以及它们在宏观方案中存在的位置。这实际上可能更多是范围问题。您是否尝试在两个不同的请求中访问最后一个插入ID?

您能解释一下您的申请流程及其与您的问题的关系吗?

答案 3 :(得分:9)

为了让getLastInsertId()返回一个值,您需要进行插入(或更新,我相信)。你能粘贴更多代码吗?

如果您从另一个控制器函数调用该函数,您也可以使用$this->Form->id来获取所需的值。

答案 4 :(得分:9)

这是查找最后插入ID的最佳方法。

$this->ModelName->getInsertID();

其他方式是使用

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

答案 5 :(得分:8)

使用save方法

时,有几种方法可以获取最后插入的主键ID
$this->loadModel('Model');
$this->Model->save($this->data);

这将返回模型当前模型的最后插入ID

$this->Model->getLastInsertId();
$this->Model-> getInsertID();

这将返回给定模型名称

的模型的最后插入ID
$this->Model->id;

这将返回上次加载的模型的最后插入ID

$this->id;

答案 6 :(得分:6)

尝试在模型类中使用此代码(可能在AppModel中):

function get_sql_insert_id() {
   $db =& ConnectionManager::getDataSource($this->useDbConfig);
   return $db->lastInsertId();
}

警告说明:MySql的LAST_INSERT_ID()函数仅适用于具有AUTO_INCREMENT字段的表(否则它只返回0)。如果主键没有AUTO_INCREMENT属性,则可能是导致问题的原因。

答案 7 :(得分:6)

尝试使用此代码。尝试将其设置为变量,以便您可以在其他函数中使用它。 :)

$variable = $this->ModelName->getLastInsertId();

在PHP原生中,试试这个。

$variable = mysqli_insert_id();

答案 8 :(得分:4)

以下是选项:

echo $this->Registration->id;

echo $this->Registration->getInsertID();

echo $this->Registration->getLastInsertId();

在此,您可以将Registration替换为您的型号名称。

由于

答案 9 :(得分:4)

这将返回上次加载的模型的最后插入ID

$this->id;

这将返回给定模型名称

的模型的最后插入ID
$this->Model->id;

这将返回模型当前模型的最后插入ID

CakePHP有两种获取最后插入ID的方法: Model::getLastInsertID()Model::getInsertID()

echo $this->ModelName->getInsertID();
echo $this->ModelName->getLastInsertID();

答案 10 :(得分:3)

你可以通过多种方式获取最后一个被密送的id。像模型名称是User,所以获取最后一个插入的id的最佳方法是

$this->User->id; // For User Model

您也可以使用模型函数,但是下面的代码将返回给定模型名称的模型的最后插入ID,对于此示例,它将返回用户模型数据

$this->User->getLastInsertId();
$this->User->getInsertID();

答案 11 :(得分:3)

使用save()时,最后一个插入ID设置为模型的$id属性。所以:

if ($this->Model->save()) {
    printf('Last insert ID was %s', $this->Model->id);
}

答案 12 :(得分:3)

每次在模型上调用save方法时,cake都在内部调用Model :: getLastInsertId()并将结果存储到模型类属性id中,因此在调用save()之后不必调用Model :: getLastInsertId( )或inserId(),因为可以像这样直接访问该值

$id = $this->id;// within a model
$id = $this->{$this->modelName}->id;// in a controller

答案 13 :(得分:3)

使用此

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

}

答案 14 :(得分:2)

插入数据后,我们可以使用以下代码来获取最近添加的记录的ID:

    $last_insert_id=$this->Model->id;

答案 15 :(得分:2)

每次在任何模型上执行插入操作时,cake都在内部提取最后一个插入ID并设置为Model-> id属性。

所以可以通过$ Model-> id;直接访问它, 无需再次查询lastInsertId。

答案 16 :(得分:2)

如果您在MySQL数据库中使用InnoDB Tables,我认为它适用于getLastInsertId()。您也可以使用$this->Model->id

答案 17 :(得分:1)

实际上你是以错误的方式使用getLastInsertId或getInsertId。 getLastInsertId()只能在save()方法之后工作。 在手动插入后它甚至不起作用,因为cake引擎将mysql_insert_id存储在save方法中的$ this-> _insertID下,可以通过getLastInsertId或getInsertId检索。 现在你的情况

$this->Model->id

OR

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

会这样做。

答案 18 :(得分:1)

这很有意思,我也偶然发现了这个问题。您问的可能是如何获取某个模型的最后一个ID而不管它是什么状态,是否刚刚插入。为了进一步了解getInsertID的作用,我们需要查看来源:

Link 1: http://api20.cakephp.org/view_source/model#line-3375

public function getInsertID() {
  return $this->_insertID
}

是的,这是该功能中唯一的一段代码。这意味着cakephp缓存任何最后插入的ID,而不是从数据库中检索它。这就是为什么如果你在模型上没有创建任何记录时使用该功能,你什么也得不到。

我做了一个小函数来获取某个表的最后一个ID,但请注意,这不应该用作getLastID()getLastInsertID()的替代,因为它有完全不同的用途

将函数lastID()添加到AppModel,如下所示,以便可以在系统范围内使用它。它具有限制,不能在具有复合主键的模型上使用。

class AppModel extends Model {
  public function lastID() {
    $data = $this->find('first', 
      array(
        'order' => array($this->primaryKey . ' DESC'),
        'fields' => array($this->primaryKey)
      )
    );

    return $data[$this->name][$this->primaryKey];
  }
}

Original Source : Class Model

答案 19 :(得分:1)

在CakePHP中你可以通过以下方式获得: Model :: getInsertID()//返回此模型插入的最后一条记录的ID。 Model :: getLastInsertID()// getInsertID()的别名。

答案 20 :(得分:1)

$Machinedispatch = 
   $this->Machinedispatch->find('first',array('order'=>array('Machinedispatch.id DESC')));

查找最后插入行的最简单方法。对我来说getLastInsertId()这不起作用。

答案 21 :(得分:0)

$this->Model->field('id', null, 'id DESC')