S
S
Stepan2015-02-25 14:40:14
Yii
Stepan, 2015-02-25 14:40:14

(Yii2) How to save multiple rows to the database at once?

Did something similar to this
b39dc0d97c97481688a965dc6223e9e5.png
. All users are displayed from the database. Click to edit the fields.
I assign names to inputs which should be Users[name] depending on the field type.
But there is no way to save everything if several fields are edited

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vyacheslav Barsukov, 2015-02-25
@slavabars

Well, you cycle through the array and break it into queries.
Why shove everything in 1 request? Yes, and you can do only one insert, update will not work.

S
Stepan, 2015-02-25
@xoma2

Made it like this
I wrap it in input and add the post id to the field type.
Then I sort in the controller

foreach($_POST['Users'] as $key=>$value){
                list($id, $name) = explode("_", $key);
                $user = Users::findOne($id);
                $user->$name = $value;
                $user->save();
}

R
raf_nnov, 2015-02-26
@raf_nnov

www.yiiframework.com/doc-2.0/guide-input-tabular-i...
I can show on my editing example (it doesn’t differ much from the docks)...
though I don’t edit the fields individually...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question