php 使用学说 2 获取最后一个插入 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3509350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the last insert id with doctrine 2?
提问by tom
How can I get the last insert id with doctrine 2 ORM? I didn't find this in the documentation of doctrine, is this even possible?
我怎样才能得到最后一个插入 id 与学说 2 ORM?我在教义文档中没有找到这个,这甚至可能吗?
回答by tom
I had to use this after the flush to get the last insert id:
我必须在刷新后使用它来获取最后一个插入 ID:
$em->persist($user);
$em->flush();
$user->getId();
回答by clarkstachio
You can access the id after calling the persist method of the entity manager.
调用实体管理器的persist方法后就可以访问id了。
$widgetEntity = new WidgetEntity();
$entityManager->persist($widgetEntity);
$entityManager->flush();
$widgetEntity->getId();
You doneed to flush in order to get this id.
您确实需要刷新才能获得此 ID。
Syntax Error Fix: Added semi-colon after $entityManager->flush() is called.
语法错误修复:在调用 $entityManager->flush() 后添加分号。
回答by Francesco Casula
If you're not using entities but Native SQL as shown herethen you might want to get the last inserted id as shown below:
如果您使用的不是实体而是本机 SQL,如下所示,那么您可能希望获取最后插入的 id,如下所示:
$entityManager->getConnection()->lastInsertId()
For databases with sequences such as PostgreSQLplease note that you can provide the sequence name as the first parameter of the lastInsertId
method.
对于PostgreSQL等具有序列的数据库,请注意您可以提供序列名称作为方法的第一个参数lastInsertId
。
$entityManager->getConnection()->lastInsertId($seqName = 'my_sequence')
For more information take a look at the code on GitHub hereand here.
回答by beberlei
Calling flush() can potentially add lots of new entities, so there isnt really the notion of "lastInsertId". However Doctrine will populate the identity fields whenever one is generated, so accessing the id field after calling flush will always contain the ID of a newly "persisted" entity.
调用 flush() 可能会添加大量新实体,因此实际上并没有“lastInsertId”的概念。然而,无论何时生成,Doctrine 都会填充身份字段,因此在调用 flush 后访问 id 字段将始终包含新“持久化”实体的 ID。
回答by Purasapa
A bit late to answer the question. But,
回答问题有点晚了。但,
If it's a MySQL database
如果是 MySQL 数据库
should $doctrine_record_object->id
work if AUTO_INCREMENT
is defined in database and in your table definition.
$doctrine_record_object->id
如果AUTO_INCREMENT
在数据库和表定义中定义,应该可以工作。
回答by Techappri
Here i post my code, after i have pushed myself for one working day to find this solution.
在我强迫自己一个工作日找到这个解决方案之后,我在这里发布了我的代码。
Function to get the last saved record :
获取最后保存的记录的函数:
private function getLastId($query) {
$conn = $this->getDoctrine()->getConnection();
$stmt = $conn->prepare($query);
$stmt->execute();
$lastId = $stmt->fetch()['id'];
return $lastId;
}
Another Function which call the above function
调用上述函数的另一个函数
private function clientNum() {
$lastId = $this->getLastId("SELECT id FROM client ORDER BY id DESC LIMIT 1");
$noClient = 'C' . sprintf("%06d", $lastId + 1); // C000002 if the last record ID is 1
return $noClient;
}
回答by Olivier Bussier
More simple: SELECT max(id) FROM client
更简单: SELECT max(id) FROM client