【问题标题】:Doctrine - insert multiple rows with just one save()教义 - 只用一个 save() 插入多行
【发布时间】:2011-03-24 06:51:58
【问题描述】:

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

【问题讨论】:

    标签: php database insert doctrine save


    【解决方案1】:

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

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

    这仅适用于所有记录都针对同一个表的情况。否则你就不走运了。

    【讨论】:

    • 谢谢,正是我想要的。
    • 这太完美了!插入 1000 行只用了 3.8 秒而不是 76 秒!!
    • 无论如何要先插入一条记录并获取它的 id 并添加其余部分并插入所有其他记录
    【解决方案2】:

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

    【讨论】:

    • 我很困惑。你说不需要实例化新记录,但是当你反复说$row = new My_Doctrine_Record();时,这正是你在代码中所做的,对吧?
    • ops,我的意思是“不需要保存每条记录”。我会改的
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

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

        这应该如何用一个 mysql INSERT 插入所有记录?

        【讨论】:

        • 您在 Collection.php 中的摘录由$conn->beginInternalTransaction()$conn->commit() 包装。这或许可以解释。
        • 感谢您的评论,但例如:MySQL MyISAM 不支持事务...
        猜你喜欢
        • 2018-01-30
        • 1970-01-01
        • 2015-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-09
        • 1970-01-01
        相关资源
        最近更新 更多