Answer the question
In order to leave comments, you need to log in
Why is it stored like this in the database?
there is a Product table with one of the columns tied, it is assumed that related products will be entered there, so its filling is set in the form
<?php
$product=Product::find()->where('category_id='.$cat->id)->all();
foreach($product as $prod){ ?>
<?= $form->field($model, 'tied['.$prod->title.']')->checkbox(['value' => $prod->title, 'label' => $prod->title])
}
?>
if ($model->load(Yii::$app->request->post())) {
$post=Yii::$app->request->post();
foreach($post ["Product"]["tied"] as $tied){
$model->tied .=$tied."%";
}
$model->save();
}
Answer the question
In order to leave comments, you need to log in
I understand that you have one class used i.e. just a product?
If so, then mass work and updates are carried out like this.
In controller:
public function actionUpdate()
{
// Тут получаете свой cat либо передаете каким либо образом.
$products = Product::find()->where('category_id'= $cat->id)->indexBy('id')->all();
if (Model::loadMultiple($products, Yii::$app->request->post()) && Model::validateMultiple($products)) {
foreach ($products as $product) {
$product->save(false);
}
return $this->redirect('index');
}
return $this->render('update', ['products' => $products]);
}
$form = ActiveForm::begin();
foreach ($products as $index => $product) {
echo $form->field($product, "[$index]value")->checkbox(['value' => $prod->title, 'label' => $prod->title]);
}
ActiveForm::end();
//получаете продукты и связи за один запрос.
$products = Product::find()->with('category', 'someTied')->all();
//или с условием
$products = Product::find()->with([
'categpry' => function ($query) {
$query->andWhere(['category_id' => 15]);
},
])->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question