Hello,
I would like to know if I could get a little bit of a push on this please as I onlyneed to understand the logic, then I will be ok.
When you have for example 2 tables:
-Posts
-Categories
In my MYSQL Benchmark software I created these 2 tables and linked them as:
Categories "can have many posts" by using the l:n button.
Now in Yii2 I have generated the model and CRUD for each.
Here is where I am a bit confused:
1)if I want to create a post but have the drop down menu from the categories model in the form, how to output the dropdown menu data, I have the code below:
in the Categories Model I have added this:
use yii\helpers\ArrayHelper;
public function getDataList() { // could be a static func as well
$models = Categories::find()->asArray()->all();
return ArrayHelper::map($models, 'id', 'categories');
}
Now in the Posts Model I wanted to add this:
echo $form->field($model, 'field')->dropDownList($model->dataList);
But how to add this line here:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'data:ntext',
'$form->field($model, 'field')->dropDownList($model->dataList);',<---this causes an error
'created_at',
'updated_at',
],
]) ?>
My second problem is let’s say the above get resolved and I start seeing the drop down menu of all the data inside the table “categories”, how will this drop down menu input be saved in the database?
If you could explain to me the above, it will really help me during the learning phase of Yii2.
Thank you so much!
Ben