Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you insert multiple rows in a loop using doctrine 2

i want to insert multiple rows in a loop using doctrine 2..

i usually insert 1 record using this:

$Entity->setData($posted); $this->_doctrine->persist($Entity); $this->_doctrine->flush();

like image 427
dean jase Avatar asked Oct 11 '22 21:10

dean jase


1 Answers

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)

like image 62
Michael Ridgway Avatar answered Jan 01 '23 11:01

Michael Ridgway