Doctrine2和原始sql:获取最后插入的ID



我有这样的东西:

$stmt = $this->getDoctrine()->getConnection()->prepare(
        'insert into someTable (columnList) values (parameters);');
/* 
bind parameters
*/
$stmt->execute();

如何获取最后插入的ID?

谢谢,Scott

您需要使用lastInsertId()。

示例:

$dbh = Doctrine_Manager::getInstance()->getCurrentConnection();
$sth = $dbh->prepare("INSERT INTO continent (created_at, updated_at) VALUES ( NOW(), NOW() )");
$sth->execute();
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$conn->lastInsertId('continent_id');

下面的代码正确回答了这个问题:

//This call will get the doctrine connection from inside a symfony2 controller
$conn = $this->getDoctrine()->getConnection();
$stmt = $conn->prepare(
        'insert into someTable (columnList) values (parameters);');
/* 
bind parameters
*/
$stmt->execute();
$id = $conn->lastInsertId();

要从您创建的条令存储库类内部获得连接:

$conn = $this->getEntityManager()->getConnection();

相关内容

  • 没有找到相关文章

最新更新