2

私はこのようなものを持っています:

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

/* 
bind parameters
*/

$stmt->execute();

最後に挿入された ID を取得するにはどうすればよいですか?

ありがとう、スコット

4

2 に答える 2

9

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');
于 2012-08-20T19:13:43.267 に答える
2

以下のコードは、質問に正しく答えます。

//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();

作成した doctrine リポジトリ クラス内から接続を取得するには:

$conn = $this->getEntityManager()->getConnection();
于 2016-01-02T07:01:07.967 に答える