Showing Dropdown Menu From Another Model


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:



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' => [




            '$form->field($model, 'field')->dropDownList($model->dataList);',<---this causes an error




    ]) ?>

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!


You may need to do some reading on the yii DetailView. You cannot pass attributes like the way you have done. You cannot directly refer an ActiveForm field inside the way you have done.

You need to use array format for attributes, for example… something like this is possible:

['attribute'=>'field', 'format'=>'raw', 'value'=>Html::dropdownList($list, $options)]

To add, normally the detail view allows you to view a model. If you really are looking premade solution to both view and edit data using DetailView… you can try this yii2-detail-view extension.

Hi Kartik,

Thank you for the reply.

I am not sure this answer my question sorry.

I now understood I made a mistake in writing the code, thanks for that.

But regarding the whole thing I am still a bit lost as I have seen your exntension, fine but I cannot understand how could this help me as I believe that what I am trying to achieve is pretty basic.

All I am trying to do is post a drop down menu that contains my categories names into the Posts model so when someone writes a Post,he can choose a category at the same time.

My other question was, how can I save the data coming from this drop down menu into the database:

1)Shall I create a new field in the Posts table called "categories" and register the data in this field.


2)Create with mysqlbenchmark a relation 1 to MANY(1:n) from the Categories to the Posts table?

Thanks again,


You may want to start by generating a CRUD for your Post Model using Gii. Here is the guide to using the gii tool. If you have not done this, try doing this first. If you have already done this… then you can then take a look at the _form.php in the post views folder. Within that change your category_id field to use a dropdownList. That’s a simple way to start this and build upon it.

Also you may want to first read and brush upon the MVC concepts (if not done already) to understand how you work with the model, view, and controller in Yii