Answer the question
In order to leave comments, you need to log in
Updating data in a database?
The whole essence of this story is as follows: I display data from the database through GridView. I display checkboxes in the column. And through dropDownList I display the list of categories.
Html::dropDownList('records','id',
ArrayHelper::map($listData,'id', 'name'),
['prompt' => '-- Выбрать категорию --']);
(
[0] => 1
[1] => 3
[2] => 5
)
if ($records = Yii::$app->getRequest()->post('records')
&& $selection = Yii::$app->request->post('selection')) {
foreach($selection as $id){
$e = Book::findOne((int)$id);
$e->save();
}
}
Answer the question
In order to leave comments, you need to log in
$records = Yii::$app->getRequest()->post('records');
$selection = Yii::$app->request->post('selection');
if (!is_null($records) && !is_null($selection)) {
Book::updateAll(['category_id' => $selection], ['id' => $records]);
}
if
then why are you overthinking? If this is a string
If I understand correctly, then it should be:
$id = $records[$selection];
if($e = Book::findOne($id)){
$e->save();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question