Answer the question
In order to leave comments, you need to log in
How to display data from a linked table in dropDownLIst in yii2?
Good evening.
There was a small issue with outputting data to a dropDownList from a related table.
There are three tables in the database:
1) Recipe // table with recipes
2) Ingredients // table with ingredients
3) RecipesIngredients // link table, store id of recipes and ingredients
Links are configured in the models:
// Recipe model
public function getIngred()
{
return $this->hasMany(Ingredients::className(), ['id' => 'ingredient_id'])->viaTable('{{%recipes_ingredients}}', ['recipe_id' => 'id']);
}
// Ingredients
public function getRecipe()
{
return $this->hasMany(Recipe::className(), ['id' => 'recipe_id'])->viaTable('{{%recipes_ingredients', ['ingredient_id' => 'id']);
}
public static function getActiveIngred()
{
return ArrayHelper::map(self::find()->where(['status' => self::STATUS_ACTIVE])->orderBy('name')->all(), 'id', 'name');
}
public function actionCreate()
{
$model = new Recipe();
for($i=0; $i<5; $i++){
$ingredient[] = new Ingredients();
}
$ingreds = Yii::$app->request->post('Ingredients');
if ($model->load(Yii::$app->request->post()) && $model->save()) {
foreach($ingreds as $ingred){
$recipe_ingred = new RecipesIngredients();
$recipe_ingred->recipe_id = $model->id;
$recipe_ingred->ingredient_id = $ingred['name'];
$recipe_ingred->save();
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'ingredient' => $ingredient
]);
}
}
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?php
foreach($ingredient as $key => $value){
echo $form->field($value, '['.$key.']name')->dropDownList(Ingredients::getActiveIngred(), ['prompt' => 'Выбрать']);
}
?>
<?= $form->field($model, 'status')->dropDownList(Recipe::getStatusesArray()) ?>
<?= Html::submitButton($model->isNewRecord
? 'Сохранить'
: 'Обновить',
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
Answer the question
In order to leave comments, you need to log in
The solution is:
foreach($ingredient as $key => $value){
echo $form->field($value, '['.$key.']name')->dropDownList(Ingredients::getActiveIngred(),
!$value->isNewRecord ?
[
'options' => [
$value->id => ['selected' => true]
]
]
: ['prompt' => 'Выбрать']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question