yii-dropdown值未设置为db

时间:2012-06-13 08:35:19

标签: yii

我的2个相关下拉列表exam_type和状态工作正常,它们显示值..但这些值没有设置成数据库..plz帮助我...这是我的代码

表单视图:


   <tr>
<td><?php echo $form->labelEx($model,'exam type :'); ?></td>

<td> 

   <?php echo   CHtml::dropDownList('exam_type','',CHtml::listData(class1::model()->findAll(),'class','class'),array('empty'=>'Choose one',

'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicstates'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#status', //selector to update


)));

//empty since it will be filled by the other dropdown
?>


</td>

<td>    <?php echo $form->error($model,'exam_type'); ?></td>

</tr>
 <tr>
<td><?php echo $form->labelEx($model,'status :'); ?></td>
<td><?php echo CHtml::dropDownList('status','', array());?></td>
<td>    <?php echo $form->error($model,'status'); ?></td>

</tr>

控制器视图:

public function actiondynamicstates()
{

echo $aasd=$_POST['exam_type'];
 echo $data=admission::model()->findAll('class=:class',
              array(':class'=>$aasd));


$data=CHtml::listData($data,'studentid','studentfname');

    foreach($data as $value=>$name)
            echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);

}

3 个答案:

答案 0 :(得分:1)

很可能是下拉列表中的问题。你应该替换

CHtml::dropDownList('exam_type'

CHtml::dropDownList(CHtml::activeName($model, 'exam_type'

CHtml::dropDownList('status'

CHtml::dropDownList(CHtml::activeName($model, 'status'

答案 1 :(得分:1)

在我遇到这个问题之前几天。我做了一些工作。

   <tr>
<td><?php echo $form->labelEx($model,'exam type :'); ?></td>

<td> 

<?php echo   CHtml::dropDownList('exam_type','',CHtml::listData(class1::model()->findAll(),'class','class'),array('empty'=>'Choose one',

'ajax' => array(
'type'=>'POST', //request type
 'url'=>CController::createUrl('dynamicstates'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
// 'update'=>'#status', //selector to update
 'update'=>'#modelname_status', //here is the trick replace modelname to orginal model class name 


  )));

 //empty since it will be filled by the other dropdown
 ?>
 </td>

 <td>    <?php echo $form->error($model,'exam_type'); ?></td>

 </tr>
 <tr>
 <td><?php echo $form->labelEx($model,'status :'); ?></td>
  <?php //echo CHtml::dropDownList('status','', array());?>
 <td><?php echo CHtml::dropDownList('modelname[status]','', array());?></td>//same here
   <td>    <?php echo $form->error($model,'status'); ?></td>

    </tr>

在上面的代码中我改变了:

 'update'=>'#modelname_status', //here is the trick replace modelname to orginal model class name 

 <td><?php echo CHtml::dropDownList('modelname[status]','', array());?></td>//same here

即使它没有工作给予评论。(我没有测试,也许你的控制器似乎有问题。)

答案 2 :(得分:0)

我希望你的问题已经解决了。如果有人仍然想知道为什么这些下拉列表不更新数据库,则不会因为它们未被列为安全属性。 下面是一个使下拉列表工作的示例(仅给出重要部分):

假设您要添加名为“showOnHome”的字段。首先,在数据库表中创建一个名为“showOnHome”的字段(我更喜欢int(1)作为字段类型)。

然后,在您的模型中:

 public function rules() {
....
....
array('showOnHome','safe'),
}

....
....
 public function attributeLabels() {
    return array(
    .....
    .....
    .....
   'showOnHome' => 'Show On Homepage' ,

   )
}

在您的视图中

<div class="row">
        <?php echo $form->labelEx($model,'showOnHome'); ?>
        <?php echo $form->dropDownList($model, 'showOnHome', array('0'=>'No','1'=>'Yes') , array('empty'=>'--Select--')); ?>
        <?php echo $form->error($model,'showOnHome'); ?>
</div>

如果你没有改变Yii生成的默认控制器代码,上面的代码snippsets应该可以完成你的工作,例如

/**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $model=new Livefuture;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Livefuture']))
        {
            $model->attributes=$_POST['Livefuture'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Livefuture']))
        {
            $model->attributes=$_POST['Livefuture'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }