How to add data in batches in Yii2 and add data in batches in Yii2

Source: Internet
Author: User

How to add data in batches in Yii2 and add data in batches in Yii2

Batch add this operation is often used in actual development. Today, I will take the time to sort out some questions about batch add in yii2. Let's take a look at it with interested friends.

In the previous article, I introduced the tutorial on batch deletion of Yii2 gridview. Of course, the focus is on how to operate the gridview. Now let's talk about how to add data in batches in yii2?

This is not easy. I use foreach to insert data directly into the database in each loop, which is simple and rude! If you are in a company with me, I don't think it would be possible to see you the next day!

Let's not talk much about it. If you scold me too much, let's get into the question. Let's first look at a table structure that is simple to what pupils know.

//test id name

Now we want to insert 10 data records in batch for this data table in yii2.

The method we want must be the following: how straightforward an SQL statement is.

insert into test (name) values ('zhangsan'), ('lisi');

The analysis is complete. All right, let's take a look at the specific implementation.

// Assume $ names = ['hangsan', 'lisi']; $ data = []; foreach ($ names $ k => $ v) {$ data [] = [$ v];} Yii: $ app-> db-> createCommand ()-> batchInsert ('test', ['name'], $ data)-> execute ();

I believe that many people aim at whether AR can be inserted in batches. The reason is that it is safer and easier to operate. But the official Manual does not seem to exist. No, no... My heart is broken. I didn't find it. At least I didn't find it. If you find it, click the original text to find me and contact me. I also need a method for si.

Unfortunately, I found an operation method associated with AR. Let's share it and see how it works.

Suppose there is a Post class array $ models, you can do this

Use yii \ helpers \ ArrayHelper; $ rows = []; foreach ($ models as $ model) {if ($ model-> validate ()) {$ rows [] = $ model-> attributes; }}$ rows = ArrayHelper: getColumn ($ models, 'bubutes '); $ postModel = new Post; Yii :: $ app-> db-> createCommand ()-> batchInsert (Post: tableName (), $ postModel-> attributes (), $ rows)-> execute (); // Of course, all the fields are inserted, but the facts are often counterproductive and simple, make a slight adjustment to $ rows [] = ['title' => $ model-> title, 'content' => $ model-> content,]; Yii :: $ app-> db-> createCommand ()-> batchInsert (Post: tableName (), ['title', 'content'], $ rows)-> execute ();

Although it is back to batchInsert, it does not matter. The verification is verified and there is no need to worry about security.

The above section describes how to add data in batches in Yii2. I hope it will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.