5

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();

2 Answers 2

4

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.