<bdo id='cUFzK'></bdo><ul id='cUFzK'></ul>
    1. <small id='cUFzK'></small><noframes id='cUFzK'>

    2. <legend id='cUFzK'><style id='cUFzK'><dir id='cUFzK'><q id='cUFzK'></q></dir></style></legend>
    3. <i id='cUFzK'><tr id='cUFzK'><dt id='cUFzK'><q id='cUFzK'><span id='cUFzK'><b id='cUFzK'><form id='cUFzK'><ins id='cUFzK'></ins><ul id='cUFzK'></ul><sub id='cUFzK'></sub></form><legend id='cUFzK'></legend><bdo id='cUFzK'><pre id='cUFzK'><center id='cUFzK'></center></pre></bdo></b><th id='cUFzK'></th></span></q></dt></tr></i><div id='cUFzK'><tfoot id='cUFzK'></tfoot><dl id='cUFzK'><fieldset id='cUFzK'></fieldset></dl></div>
      <tfoot id='cUFzK'></tfoot>

      1. Doctrine2 - 一次多次插入

        Doctrine2 - Multiple insert in one shot(Doctrine2 - 一次多次插入)

          <small id='y4IPh'></small><noframes id='y4IPh'>

          <legend id='y4IPh'><style id='y4IPh'><dir id='y4IPh'><q id='y4IPh'></q></dir></style></legend>

            1. <tfoot id='y4IPh'></tfoot>

              <i id='y4IPh'><tr id='y4IPh'><dt id='y4IPh'><q id='y4IPh'><span id='y4IPh'><b id='y4IPh'><form id='y4IPh'><ins id='y4IPh'></ins><ul id='y4IPh'></ul><sub id='y4IPh'></sub></form><legend id='y4IPh'></legend><bdo id='y4IPh'><pre id='y4IPh'><center id='y4IPh'></center></pre></bdo></b><th id='y4IPh'></th></span></q></dt></tr></i><div id='y4IPh'><tfoot id='y4IPh'></tfoot><dl id='y4IPh'><fieldset id='y4IPh'></fieldset></dl></div>

                  <bdo id='y4IPh'></bdo><ul id='y4IPh'></ul>
                    <tbody id='y4IPh'></tbody>
                • 本文介绍了Doctrine2 - 一次多次插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Doctrine 的新手,对我来说仍然有一些模糊的地方.在这种情况下,我使用循环和实体管理器在数据库中插入新记录.它工作正常,但我注意到 Doctrine 按实体创建一个插入查询,这可能会变得非常庞大.

                  I'm new to Doctrine and there are still some blurred areas for me. In this case I'm inserting new record in the database using a loop and the entity manager. It works fine but I noticed that Doctrine make one insert query by entity, which can become pretty huge.

                  使用 Doctrine2 和 Symfony 2.3,我想知道我们如何设置它,以便它只使用其中的所有值进行 1 个插入查询(我们当然只讨论 1 个实体).

                  Using Doctrine2 and Symfony 2.3, I would like to know how we can set it up so it would make only 1 insert query with all the values in it (we are talking of 1 entity only of course).

                  我的意思是改变这个:

                  INSERT INTO dummy_table VALUES (x1, y1)    
                  INSERT INTO dummy_table VALUES (x2, y2)
                  

                  进入

                  INSERT INTO dummy_table VALUES (x1, y1), (x2, y2)
                  

                  这是我的代码:

                  $em = $this->container->get('doctrine')->getManager();
                  
                  foreach($items as $item){
                      $newItem = new Product($item['datas']);
                      $em->persist($newItem);
                  }
                  
                  $em->flush();
                  

                  推荐答案

                  根据 这个答案,Doctrine2 不允许你将多个 INSERT 语句组合成一个:

                  According to this answer, Doctrine2 does not allow you to combine multiple INSERT statements into one:

                  有些人似乎想知道为什么 Doctrine 不使用多插入(插入 (...) 值 (...), (...), (...), ...

                  Some people seem to be wondering why Doctrine does not use multi-inserts (insert into (...) values (...), (...), (...), ...

                  首先,这个语法只在mysql及更新版本上支持PostgreSQL 版本.其次,没有简单的方法可以掌握所有使用时在这种多插入中生成的标识符AUTO_INCREMENT 或 SERIAL 和 ORM 需要标识的标识符对象的管理.最后,插入性能很少是ORM 的瓶颈.普通的插入速度足够快大多数情况下,如果你真的想做快速批量插入,那么无论如何,多插入不是最好的方法,即 Postgres COPY 或 MysqlLOAD DATA INFILE 快了几个数量级.

                  First of all, this syntax is only supported on mysql and newer postgresql versions. Secondly, there is no easy way to get hold of all the generated identifiers in such a multi-insert when using AUTO_INCREMENT or SERIAL and an ORM needs the identifiers for identity management of the objects. Lastly, insert performance is rarely the bottleneck of an ORM. Normal inserts are more than fast enough for most situations and if you really want to do fast bulk inserts, then a multi-insert is not the best way anyway, i.e. Postgres COPY or Mysql LOAD DATA INFILE are several orders of magnitude faster.

                  这些是不值得努力实施一个在 mysql 和 postgresql 上执行多插入的抽象ORM.

                  These are the reasons why it is not worth the effort to implement an abstraction that performs multi-inserts on mysql and postgresql in an ORM.

                  您可以在此处阅读有关 Doctrine2 批处理的更多信息:http://www.doctrine-project.org/blog/doctrine2-batch-processing.html

                  You can read more about Doctrine2 batch processing here: http://www.doctrine-project.org/blog/doctrine2-batch-processing.html

                  您可以切换到 DBAL 或通过在一定数量的插入后刷新实体管理器来小批量处理您的数据:

                  You can either switch to DBAL or resort to processing your data in small batches by flushing your entity manager after a set amount of inserts:

                  $batchSize = 20;
                  
                  foreach ($items as $i => $item) {
                       $product = new Product($item['datas']);
                  
                       $em->persist($product);
                  
                       // flush everything to the database every 20 inserts
                       if (($i % $batchSize) == 0) {
                           $em->flush();
                           $em->clear();
                      }
                  }
                  
                  // flush the remaining objects
                  $em->flush();
                  $em->clear();
                  

                  这篇关于Doctrine2 - 一次多次插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  • <bdo id='r5ZD3'></bdo><ul id='r5ZD3'></ul>
                      <tbody id='r5ZD3'></tbody>
                    <tfoot id='r5ZD3'></tfoot>
                        <i id='r5ZD3'><tr id='r5ZD3'><dt id='r5ZD3'><q id='r5ZD3'><span id='r5ZD3'><b id='r5ZD3'><form id='r5ZD3'><ins id='r5ZD3'></ins><ul id='r5ZD3'></ul><sub id='r5ZD3'></sub></form><legend id='r5ZD3'></legend><bdo id='r5ZD3'><pre id='r5ZD3'><center id='r5ZD3'></center></pre></bdo></b><th id='r5ZD3'></th></span></q></dt></tr></i><div id='r5ZD3'><tfoot id='r5ZD3'></tfoot><dl id='r5ZD3'><fieldset id='r5ZD3'></fieldset></dl></div>

                        <small id='r5ZD3'></small><noframes id='r5ZD3'>

                            <legend id='r5ZD3'><style id='r5ZD3'><dir id='r5ZD3'><q id='r5ZD3'></q></dir></style></legend>