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

    1. <i id='ltEER'><tr id='ltEER'><dt id='ltEER'><q id='ltEER'><span id='ltEER'><b id='ltEER'><form id='ltEER'><ins id='ltEER'></ins><ul id='ltEER'></ul><sub id='ltEER'></sub></form><legend id='ltEER'></legend><bdo id='ltEER'><pre id='ltEER'><center id='ltEER'></center></pre></bdo></b><th id='ltEER'></th></span></q></dt></tr></i><div id='ltEER'><tfoot id='ltEER'></tfoot><dl id='ltEER'><fieldset id='ltEER'></fieldset></dl></div>
    2. <small id='ltEER'></small><noframes id='ltEER'>

      <tfoot id='ltEER'></tfoot>

        • <bdo id='ltEER'></bdo><ul id='ltEER'></ul>

        ActiveRecord 批量插入 (yii2)

        ActiveRecord batch insert (yii2)(ActiveRecord 批量插入 (yii2))
        1. <tfoot id='2ymK8'></tfoot>

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

              <small id='2ymK8'></small><noframes id='2ymK8'>

                  <tbody id='2ymK8'></tbody>
                • <bdo id='2ymK8'></bdo><ul id='2ymK8'></ul>
                  <legend id='2ymK8'><style id='2ymK8'><dir id='2ymK8'><q id='2ymK8'></q></dir></style></legend>
                  本文介绍了ActiveRecord 批量插入 (yii2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以使用 Yii 的 ActiveRecord 在一个查询中插入多行?或者这只能通过较低级别的 DAO 对象实现?

                  Is it possible to insert multiple rows in one query with Yii's ActiveRecord? Or is this only possible via the lower-level DAO objects?

                  我有两个模型1- 交易2-TransactionItems

                  I have two models 1- Transaction 2-TransactionItems

                  事务项中有多行(点击添加行).

                  There are multiple rows(onclick add row) in transaction Items.

                  我想在数据库中存储多行事务项.

                  I want to store multiple rows of transactionitems in the database.

                  交易项目表截图

                  推荐答案

                  你可以使用yiidbCommandbatchInsert()方法.查看详情这里.与 ActiveRecord 一起使用时,请确保在插入前验证所有数据.

                  You can use batchInsert() method of yiidbCommand. See details here. When using it with ActiveRecord make sure validate all data before inserting.

                  假设您有一组带有 Post 类的 $models,可以这样做:

                  Assuming you have array of $models with class Post, it can be done like this:

                  $rows = [];
                  foreach ($models as $model) {
                      if (!$model->validate()) {
                          // At least one model has invalid data
                  
                          break;
                      }
                  
                      $rows[] = $model->attributes;
                  }
                  

                  如果模型不需要验证,您可以使用 ArrayHelper 缩短上面的代码以构建 $rows 数组.

                  If models don't require validation you can short the code above using ArrayHelper for building $rows array.

                  use yiihelpersArrayHelper;
                  
                  $rows = ArrayHelper::getColumn($models, 'attributes');
                  

                  然后简单地执行批量插入:

                  Then simply execute batch insert:

                  $postModel = new Post;
                  
                  Yii::$app->db->createCommand()->batchInsert(Post::tableName(), $postModel->attributes(), $rows)->execute();
                  

                  附言$postModel 仅用于提取属性名称列表,您也可以从 $models 数组中的任何现有 $model 中提取它.

                  P.S. The $postModel just used for pulling attirubute names list, you can also pull this from any existing $model in your $models array.

                  如果不需要插入所有属性,可以在填充$rows数组时指定:

                  If you don't need to insert all attributes you can specify it when filling $rows array:

                  $rows[] = [
                      'title' => $model->title,
                      'content' => $model->content,
                  ];
                  

                  不要忘记将 $postModel->attributes 替换为 ['title', 'content'].

                  Don't forget to replace $postModel->attributes to ['title', 'content'].

                  如果属性较多,您可以使用一些数组函数来指定要插入的确切属性.

                  In case of larger amount of attributes you can use some array functions to specify exact attributes for inserting.

                  这篇关于ActiveRecord 批量插入 (yii2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)
                    • <tfoot id='HPUyX'></tfoot>
                      <i id='HPUyX'><tr id='HPUyX'><dt id='HPUyX'><q id='HPUyX'><span id='HPUyX'><b id='HPUyX'><form id='HPUyX'><ins id='HPUyX'></ins><ul id='HPUyX'></ul><sub id='HPUyX'></sub></form><legend id='HPUyX'></legend><bdo id='HPUyX'><pre id='HPUyX'><center id='HPUyX'></center></pre></bdo></b><th id='HPUyX'></th></span></q></dt></tr></i><div id='HPUyX'><tfoot id='HPUyX'></tfoot><dl id='HPUyX'><fieldset id='HPUyX'></fieldset></dl></div>

                      1. <small id='HPUyX'></small><noframes id='HPUyX'>

                          <bdo id='HPUyX'></bdo><ul id='HPUyX'></ul>

                        • <legend id='HPUyX'><style id='HPUyX'><dir id='HPUyX'><q id='HPUyX'></q></dir></style></legend>

                              <tbody id='HPUyX'></tbody>