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

5om*_*ome 3 php 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));
    }
Run Code Online (Sandbox Code Playgroud)

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

      <?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(); ?>
Run Code Online (Sandbox Code Playgroud)

但我收到此错误: 未定义属性"Country.countriesIssued".

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

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

Ion*_*ian 6

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

    <?php 
echo $form->dropDownList($model, 'classification_levels_id', CHtml::listData(ClassificationLevels::model()->findAll(), 'id', 'name'),$classification_levels_options);
?>
Run Code Online (Sandbox Code Playgroud)