简体   繁体   中英

Yii2 - create dropdown (select) with another model

At the begging of the view file I am loading model:

use app\models\Countries;

And a bit down in the code I am trying to create dropdown (model is generated via gii):

<?php

        echo $form->field(Countries::find()->all(), 'Country')
                ->dropDownList(
                    ['prompt'=>'Select Country']
                );

    ?>

I am getting this error:

     PHP Fatal Error – yii\base\ErrorException
    Call to a member function formName() on a non-object

1. in /home/sasha/Documents/Scopic/Stokkee/project/svn/vendor/yiisoft/yii2/helpers/BaseHtml.php at line 1975

What is going on here?

Try this

  <?= $form->field($model, 'your_field')->dropDownList( ArrayHelper::map(Country::find()->all(), 'country_id', 'Country_description'),['prompt'=>'']) ?>

change country_id, your_field and country_description for your need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM