Yii2 How to bulk add data, YII2 Bulk add data
Add this operation in bulk, in the actual development often used to get, today's small series of time to everyone to tidy up some questions about Yii2 batch add, interested friends to see it together.
In the previous article to introduce to you about the YII2 GridView implementation of the bulk removal tutorial, of course, focus on how to operate the GridView, today we come to talk about yii2 how to add data in bulk?
There are students shouting, this is not simple, I foreach a loop, each loop inside the data inserted directly into the database, simple rough finished! I wipe, elder brother, if you are with me in a company, I think the probability of seeing you next day is not big!
Words do not say much, say more you scold me, we go to the point, first look at a simple to the pupils know the table structure
Test ID Name
We are now going to BULK insert 10 data into this data sheet in Yii2
The way we want to be is certainly the following, a SQL how simply directly
INSERT into test (name) VALUES (' Zhangsan '), (' Lisi ');
Analysis is all done, okay, just look at the concrete implementation
If $names = [' Zhangsan ', ' Lisi ']; $data = []; foreach ($names $k = = $v) {$data [] = [$v];} Yii:: $app->db->createcommand ()->batchinsert (' Test ', [' name '], $data)->execute ();
I believe that many people are directed at the AR can achieve bulk insertion, the reason is nothing more safe and more convenient operation Bai. But the Official handbook seems to have no, no, no ... Heart is broken, unexpectedly did not, at least I did not find, you found please click on the original to find me and contact me, I also si points need method ah.
But unfortunately, I found an AR associated with the operation method, we share the reference to see what's going on
Suppose you have an array of post classes $models you can do this
Use Yii\helpers\arrayhelper; $rows = []; foreach ($models as $model) {if ($model->validate ()) {$rows [] = $model->attributes;}} $rows = Arrayhelper::getcolu Mn ($models, ' attributes '); $postModel = new Post; Yii:: $app->db->createcommand ()->batchinsert (Post::tablename (), $postModel->attributes (), $rows) Execute (); Of course, the above is given to insert all the fields, but the facts often backfire, but also simple, a little adjustment can be $rows [] = [' title ' = + $model->title, ' content ' + $model->content , ]; Yii:: $app->db->createcommand ()->batchinsert (Post::tablename (), [' title ', ' content '], $rows)->execute ( );
Although back to the Batchinsert, but it does not matter, the verification has been verified that security is not to worry about.
The above is a small series to introduce you to the YII2 how to batch add data related knowledge, I hope to help you!
http://www.bkjia.com/PHPjc/1127876.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127876.html techarticle Yii2 How to add data in bulk, YII2 Bulk Add the data batch add this operation, in the actual development often used to get, today small series of time to everyone to tidy up some questions about Yii2 batch add ...