如何在symfony2中获取批量刷新的插入id



如何从大容量插入数据中获取所有插入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();
}

相关内容

  • 没有找到相关文章

最新更新