Generated for products in admin panel, model, controller and view with Gii. Changed the query to get the model:

$model = Product::find()->with('sizes')->where(['products.id' => $id])->one();

Sizes for the product get through the link:

 public function getSizes() { return $this->hasMany(Size::className(),['id' => 'size_id_ref']) ->viaTable(Value::tableName(), ['product_id_ref' => 'id']); } 

It turns out that in my $model->sizes array of objects (with id and name properties)

For sizes in ActiveForm made the withdrawal of all values ​​from the database

 <?= $form->field($model, 'pr_sizes')->checkboxList(ArrayHelper::map(\backend\models\Size::find()->all(), 'id', 'name')) ?> <?= $form->field($model, 'pr_sizes')->dropDownList(ArrayHelper::map(\backend\models\Color::find()->orderBy('name')->all(), 'id', 'name'), ['multiple'=>'multiple']) ?> 

Tell me how to make dropDownList or checkboxList with multi-selection in _form.php file, so that the dimensions corresponding to the product are immediately substituted into the form. And further so that these dimensions can be updated (write to the table of value values ​​by product id).

    1 answer 1

    In order to specify the selected items, it is necessary in the pr_sizes attribute pr_sizes list the values ​​that are selected, that is, their id .

    As an option:

     $model->pr_sizes = ArrayHelper::getColumn($model->sizes, 'id'); 

    Of course, pr_sizes should exist in your model, either be a field in the table or be declared in a class - public $pr_sizes = [];

    • good option, thanks! Now the question remains how to save changes in the checkboxes so that the data is recorded in the appropriate database table (in my cases value) - Vladimir Vasilev