从Yii中的CActiveRecord中选择或下拉列表

yAn*_*Tar 3 php yii yii1.x

我有表types,我想selectbox用这个表中的所有值构建在我的控制器中我写了这段代码

$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))
Run Code Online (Sandbox Code Playgroud)

如何在视图文件中构建selectbox?

Afn*_*hir 6

那么它非常简单你需要做的就是首先创建List Data

CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');
Run Code Online (Sandbox Code Playgroud)

对于前

typeList = CHtml::ListData(allRegistrationTypes,'id','type');
Run Code Online (Sandbox Code Playgroud)

现在记住id和type都是表中的字段

现在你所要做的就是如果你正在使用表格

<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>
Run Code Online (Sandbox Code Playgroud)

如果你需要多个,你可以将multiple => multiple数组作为htmlOptions 传入