Doctrine - 只需一次 save() 即可插入多行

发布于 2024-10-25 07:17:46 字数 73 浏览 4 评论 0原文

如何在 Doctrine 中调用一次 save() 方法将多行插入表中?

How do I insert multiple rows into table calling save() method once in Doctrine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巾帼英雄 2024-11-01 07:17:46

将每条记录添加到 Doctrine_Collection 对集合对象调用save()

$collection = new Doctrine_Collection('tablename');
$collection->add($record1);
$collection->add($record2);
$collection->add($record3);
$collection->add($record4);
$collection->save();

仅当所有记录都针对同一个表时,这才有效。否则你就不走运了。

Add each record to a Doctrine_Collection the call save() on the collection object.

$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.

惯饮孤独 2024-11-01 07:17:46

这是另一个解决方案,在 Dotrine 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();

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.

$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();
娇纵 2024-11-01 07:17:46

如果你使用 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();

If you use symfony2 it is so easy

// 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();
兔姬 2024-11-01 07:17:46

1)声明所有表。
2)创建表格。
3)发送到多个表。
4)保存数据。

use AppBundle\Entity\site;
use AppBundle\Entity\nba;

1)声明所有表。

 $site = new site;
 $nba = new nba;

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)插入到多个表中。

    {

        $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)持久化数据

            $em = $this->getDoctrine()->getManager();
            $em->persist($site);
            $em->persist($nba);
            $em->flush();

1)Declare all the tables.
2)Create the form.
3)Send to multiple tables.
4)Persist data.

use AppBundle\Entity\site;
use AppBundle\Entity\nba;

1)Declare all the tables.

 $site = new site;
 $nba = new nba;

2)Create form

$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.

    {

        $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

            $em = $this->getDoctrine()->getManager();
            $em->persist($site);
            $em->persist($nba);
            $em->flush();
不交电费瞎发啥光 2024-11-01 07:17:46

我查看了 Doctrine (1.2.x)“Collection.php”的“save”方法的代码,我看到的只是这样:

foreach ($this->getData() as $key => $record) {
   $record->save($conn);
}

如何使用一个 mysql INSERT 插入所有记录?

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:

foreach ($this->getData() as $key => $record) {
   $record->save($conn);
}

How should this ever insert all records with one mysql INSERT?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文