2011-05-31 41 views
3

我想用教條2在循環中插入多行..你如何使用學說2

在一個循環中插入多行,我通常使用這種插入1個記錄:

$實體 - >使用setData ($發佈); $ this - > _ doctrine-> persist($ Entity); $ this - > _ doctrine-> flush();

回答

2

只要堅持所有的對象,然後在循環後調用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

0

裏面你的循環,你應該能夠簡單:

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

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

$this->_doctrine->flush();