how do you insert multiple rows in a loop using doctrine 2
i want to insert multiple rows in a loop using d开发者_开发知识库octrine 2..
i usually insert 1 record using this:
$Entity->setData($posted); $this->_doctrine->persist($Entity); $this->_doctrine->flush();
Simply persist all your objects and then call flush() after the loop.
$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();
If you're inserting a large number of objects you will want to batch insert (see http://www.doctrine-project.org/docs/orm/2.0/en/reference/batch-processing.html)
Inside your loop, you should be able to simply:
$entity1->setData($data1);
$this->_doctrine->persist($entity1);
$entity2->setData($data2);
$this->_doctrine->persist($entity2);
$this->_doctrine->flush();
精彩评论