php – 使用$form-> dropdownlist的Yii下拉列表

我想从2个不同的模型创建一个表单,
一个是国家,第二个是文件.
问题是我无法制作下拉列表,我总是得到错误.

这是代码,首先是我的controller.PHP部分

$model = new Country;
$model2 = new Product;

    $this->performAjaxValidation(array($model, $model2));
    if(isset($_POST['Country'],$_POST['Product']))
    {
        // populate input data to $model and $model2
        $model->attributes=$_POST['Country'];
        $model2->attributes=$_POST['Product'];

        // validate BOTH $model and $model2
        $valid=$model->validate();
        $valid=$model2->validate() && $valid;

        if($valid)
        {
            // use false parameter to disable validation
            $model->save(false);
            $model2->save(false);

            $this->redirect('index');
        }
    }
...
$countriesIssued = Country::model()->findAll(array('select'=>'code, name', 'order'=>'name'));
...
     $this->render('legalisation', array('model'=>$model, 'model2'=>$model2, 'documents'=>$documents, 'countriesIssued'=>$countriesIssued, 'countries'=>$countries, 'flag'=>$flag));
    }

在我的视图脚本中,我使用此代码

      <?PHP $form = $this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>true,
)); ?>

<?PHP echo $form->errorSummary(array($model,$model2)); ?>

<?PHP echo $form->dropDownList($model, 'countriesIssued',
        CHtml::listData($countriesIssued, 'code', 'name'));?>


<?PHP $this->endWidget(); ?>

但我得到这个错误
未定义属性“Country.countriesIssued”.

确定它没有定义,我尝试将其更改为’countriesIssued’,然后我得到另一个错误,说为foreach()提供了无效参数.

如果有人可以帮助我.
我知道网上有更多的解决方案,我尝试但不理解,谢谢.

解决方法:

根据定义,listData的第一个参数是一个数组;你是一个对象;

    <?PHP 
echo $form->dropDownList($model, 'classification_levels_id', CHtml::listData(ClassificationLevels::model()->findAll(), 'id', 'name'),$classification_levels_options);
?>

相关文章

1、将Yii2.0advanced版中应用主体frontend或backend应用复制...
Yii2restfulAPI文档一、配置模块:1.Config/main.php:  2...
Yii在framework/i18n/data/%lang%.php文件中有很多翻译.这...
在Yii2中,官方的页面多语言解决方案有两个:方案1,使用Yii...
Yii2.0对数据库查询的一些简单的操作1234567891011121314151...
数据查询User::find()->all();此方法返回所有数据;User:...