jeesus
jeesus

Reputation: 479

YII2 simultaneous multiple row update

I have listed multiple rows of data from database in the GridView. Now I need to update certain fields (all the same type, ie having the same names but different values) in the GridView and update the values in the database. The cell in matter is returned as following:

return Html::input('text', '['.$data->country_id.']country_polity', $data->country_polity, ['class' => 'form-control']);

So in general, what I want to do is to display country polities in GridView and give user a possibility to update these values within the GridView.

I've gone through various of guides (sample below) and all the guides are about inserting the data into database, not updating. Nevertheless, I haven't been able to come up with implementations from these guides to go through with my goal.

http://www.yiiframework.com/wiki/666/handling-tabular-data-loading-and-validation-in-yii-2/

loadMultiple() doesn't return anything for me, probably due to inaccurate ways of identifying the countries that values need to be changed.

So how should I proceed? foreach every $_POSTitem and save them individually to matching DB row?

Upvotes: 3

Views: 2275

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

I think you can do so in your controller. First you can obtain the models from $_POST using

$post = Yii::$app->request->post();
$yourModels = $post['yourModelName'];

then simply

foreach ($yourModels as $model) {
            model->save(false);
}

Upvotes: 6

Related Questions