php Doctrine - 只需一次 save() 即可插入多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5415725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine - insert multiple rows with just one save()
提问by Almas Adilbek
How do I insert multiple rows into table calling save()
method once in Doctrine?
如何save()
在Doctrine 中一次将多行插入到表调用方法中?
回答by xzyfer
Add each record to a Doctrine_Collection
the call save()
on the collection object.
将每条记录添加到集合对象Doctrine_Collection
的调用中save()
。
$collection = new Doctrine_Collection('tablename');
$collection->add($record1);
$collection->add($record2);
$collection->add($record3);
$collection->add($record4);
$collection->save();
This only works if all the records are for the same table. Otherwise you're out of luck.
这只适用于所有记录都用于同一个表的情况。否则你就倒霉了。
回答by Elvis Ciotti
Here another solution ,tested on Doctrine 1.2. No need to save each records, the flush() automatically finds out all the unsaved instances and saves them all.
这是另一个解决方案,在 Doctrine 1.2 上测试过。无需保存每条记录,flush() 会自动找出所有未保存的实例并将它们全部保存。
$row = new \My_Doctrine_Record();
$row->name = 'aaa';
$row->approved = 1;
/// ...
$row = new \My_Doctrine_Record();
$row->name = 'val';
$row->approved = 'bbb';
Doctrine_Manager::connection()->flush();
回答by Serdar De?irmenci
If you use symfony2 it is so easy
如果你使用 symfony2 那就太容易了
// get the manager
$em = $this->getDoctrine()->getManager();
// enter the records
$em->persist($entitiy1);
$em->persist($entitiy2);
$em->persist($entitiy3);
$em->persist($entitiy4);
$em->persist($entitiy5);
// save the entries
$em->flush();
回答by serializer
1)Declare all the tables. 2)Create the form. 3)Send to multiple tables. 4)Persist data.
1)声明所有表。2)创建表格。3)发送到多个表。4) 持久化数据。
use AppBundle\Entity\site;
use AppBundle\Entity\nba;
1)Declare all the tables.
1)声明所有表。
$site = new site;
$nba = new nba;
2)Create form
2)创建表单
$form = $this->createFormBuilder($site)
->add('site_id', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('category', ChoiceType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px'), 'choices' => $output))
->add('team', ChoiceType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px'), 'choices' => $nbat))
->add('save', SubmitType::class, array('label' => "Create",'attr' => array('class' => 'btn btn-success', 'style' => 'margin-bottom:15px')))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
3)Insert into multiple tables.
3)插入多个表。
{
$site_id = $form['site_id']->getData();
$category = $form['category']->getData();
$team = $form['team']->getData();
$site->setSiteId($site_id);
$site->setCategory($category);
$nba->setWinner($team);
4)Persist data
4) 持久化数据
$em = $this->getDoctrine()->getManager();
$em->persist($site);
$em->persist($nba);
$em->flush();
回答by Del Pedro
I took a look into the code of the "save" method of the Doctrine (1.2.x) "Collection.php" and all I saw is something like this:
我查看了 Doctrine (1.2.x) "Collection.php" 的 "save" 方法的代码,我看到的是这样的:
foreach ($this->getData() as $key => $record) {
$record->save($conn);
}
How should this ever insert all records with one mysql INSERT?
这应该如何使用一个 mysql INSERT 插入所有记录?