How to add data in batches in Yii2, add data in batches in Yii2_PHP tutorial

WBOY
Release: 2016-07-12 08:52:07
Original
864 people have browsed it

How to add data in batches in Yii2, add data in batches in Yii2

This operation of batch adding is often used in actual development. Today, I will take the time to sort out some information about batch adding in yii2. If you are interested, please take a look at this question.

In the last article, I introduced to you the Brief Analysis of Yii2 Gridview Implementation of Batch Deletion Tutorial. Of course, the focus is on how to operate gridview. Today we will talk about how to batch add data in Yii2 ?

Some students shouted that this is not simple. I made a foreach loop and directly inserted the data into the database in each loop. It was simple and crude! Damn it, brother, if you were in the same company as me, I think the chance of seeing you the next day would be slim!

Not much to say. If you say too much, you will scold me. Let’s get down to business. Let’s first look at a table structure that is simple enough for elementary school students to recognize.

//test 
id 
name
Copy after login

We are now going to batch insert 10 pieces of data into this data table in yii2

The way we want is definitely as follows, how simple and direct a SQL statement is to solve the problem

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

The analysis is all done, okay, let’s take a look at the specific implementation

//假如 $names = ['zhangsan', 'lisi']; 
$data = []; 
foreach ($names $k => $v) { 
$data[] = [$v]; 
} 
Yii::$app->db->createCommand()->batchInsert('test', ['name'], $data)->execute();
Copy after login

I believe many people are interested in whether AR can achieve batch insertion. The reason is simply that it is safer and more convenient to operate. But there seems to be no official manual, no, no. . . My heart is broken, there is none, at least I haven't found it. If you find it, please click on the original text to find me and contact me. I also need a way.

Unfortunately, I found an operation method related to AR. Let’s share it for reference and see what’s going on

Suppose there is an array $models of Post class, you can operate like this

use yii\helpers\ArrayHelper; 
$rows = []; 
foreach ($models as $model) {
if ($model->validate()) { 
$rows[] = $model->attributes;
} 
} 
$rows = ArrayHelper::getColumn($models, 'attributes'); 
$postModel = new Post; 
Yii::$app->db->createCommand()->batchInsert(Post::tableName(), $postModel->attributes(), $rows)->execute(); 
//当然啦,上面给出的是插入所有的字段,但事实往往事与愿违,也简单,稍作调整即可 
$rows[] = [ 
'title' => $model->title, 
'content' => $model->content, 
]; 
Yii::$app->db->createCommand()->batchInsert(Post::tableName(), ['title', 'content'], $rows)->execute();
Copy after login

Although it’s back to batchInsert, it doesn’t matter. Everything that needs to be verified has been verified. There is no need to worry about security.

The above is the knowledge about how to batch add data in Yii2 introduced by the editor. I hope it will be helpful to everyone!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127876.htmlTechArticleHow to add data in batches in Yii2, add data in batches in Yii2, this operation is often used in actual development, today The editor took the time to sort out some questions about yii2 batch addition...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template