How do you insert multiple lines into a loop using doctrine 2

I want to insert several lines into a loop using doctrine 2 ..

I usually insert 1 entry using this:

$ Entity-> InstallData ($ posted); $ This is → _ doctrine-> saved ($ Entity); $ This is → _ doctrine-> flush ();

+3
source share
2 answers

Just save all your objects and then call flush () after the loop.

    $entityDataArray = array();  // let 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 are inserting a large number of objects, you will want to insert the package (see http://www.doctrine-project.org/docs/orm/2.0/en/reference/batch-processing.html )

+2
source

Inside your loop you should simply:

$entity1->setData($data1);
$this->_doctrine->persist($entity1);

$entity2->setData($data2);
$this->_doctrine->persist($entity2);

$this->_doctrine->flush();
0
source

All Articles