问题描述
||
我想使用准则2在循环中插入多行。
我通常使用以下方法插入1条记录:
$ Entity-> setData($ posted);
$ this-> _ doctrine-> persist($ Entity);
$ this-> _ doctrine-> flush();
解决方法
只需持久化所有对象,然后在循环后调用flush()即可。
$entityDataArray = array(); // let\'s assume this is an array containing data for each entity
foreach ($entityDataArray AS $entityData) {
$entity = new \\Entity();
$entity->setData($entityData);
$this->_doctrine->persist($entity);
}
$this->_doctrine->flush();
如果要插入大量对象,则需要批量插入(请参阅http://www.doctrine-project.org/docs/orm/2.0/en/reference/batch-processing.html)
, 在循环内部,您应该能够简单地:
$entity1->setData($data1);
$this->_doctrine->persist($entity1);
$entity2->setData($data2);
$this->_doctrine->persist($entity2);
$this->_doctrine->flush();