如何从大容量插入数据中获取所有插入ID?我的代码如下:
foreach ($urls as $url) {
$subDocument = new SubDocument();
$subDocument->setUrl($url);
$subDocument->setParentDocument($parentDocument);
$dm->persist($subDocument);
}
$dm->flush();
$dm->clear();
现在,我如何才能获得这些记录的插入ID?
您可以在$dm->flush();
之后访问Subdocument
的ID,因此您必须保留Subdocument
的实例以在刷新后检索其ID。
$subDocuments = array(); // init array
foreach ($urls as $url) {
$subDocument = new SubDocument();
$subDocument->setUrl($url);
$subDocument->setParentDocument($parentDocument);
$subDocuments[] = $subDocument; //added to array
$dm->persist($subDocument);
}
$dm->flush();
//now you can access IDs iterating $subDocuments like:
foreach($subDocuments as $subDocument) {
echo $subDocument->getId();
}