来自YII中数据库的DropDownList

时间:2014-02-07 11:02:27

标签: php yii

我是第一次创建yii应用程序,我使用以下代码在我的视图中创建下拉列表

echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll());

它显示错误

include(Forms.php):无法打开流:没有这样的文件或目录

C:\ wamp \ www \ LearningYii \ protected \ views \ codelookup_form.php(35):

     <?php echo $form->textField($model,'ShortDesc',array('size'=>50,'maxlength'=>50)); ?>
     <?php echo $form->error($model,'ShortDesc'); ?>
 </div>

 <div class="row">
     <?php echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll()); ?>
 </div>

 <div class="row buttons">
     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
 </div>

3 个答案:

答案 0 :(得分:2)

$opts = CHtml::listData(Codelookup::model()->findAll(),'CodeLookupId','CodeDesc');
echo $form->dropDownList($model,'ParentCodeLookupId',$opts,array('empty'=>''));     

此代码正常运行

答案 1 :(得分:1)

使用CHtml :: listData将db对象转换为列表数据:

此处ID,NAME是您的表值,应在选项值和选项文本中使用。

 <?php 
    $opts = CHtml::listData(Forms::model()->findAll(),'CodeLookupId','CodeLookupId');
    //to check list
    print_r($opts);
    echo $form->dropDownList($model,'CodeLookupId',$opts); 
 ?>

答案 2 :(得分:0)

try hope this will help..
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php
$records = loaddataform::model()->findAll();
$list = CHtml::listData($records, 'id', 'username');
echo CHtml::dropDownList('loaddataform', $model, 
      CHtml::listData($records,
      'a_id', 'a_name'),
      array('empty' => '(Select a name)'));
 ?>
 </div>


and in controller

public function actionloaddata()
{

$model = new loaddataform;
$models = loaddataform::model()->findAll(array('order' => 'a_name'));
$list = CHtml::listData($models, 'a_id', 'a_name');
$this->render('loaddata',array('model'=>$model));

}
This works correctly..it retrives name from database.