我是Yii的新手,我发现一些不好笑的事情。我有一个我想要更新的表单,每当我尝试更新它时,它总是会抛出错误。
查看: _form.php
<?php
/* @var $this BaseStationController */
/* @var $model BaseStation */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'base-station-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'c_id'); ?>
<select name="BaseStation[c_id]">
<?php foreach($models as $m):?> //Here coded the loop below to display customer's id from the database
<option value='<?php echo $m->id;?>'><?php echo $m->firstname.' '.$m->lastname;?></option>
<?php endforeach;?>
</select>
</div>
<div class="row">
<?php echo $form->labelEx($model,'base_station_num'); ?>
<?php echo $form->textField($model,'base_station_num',array('size'=>15,'maxlength'=>15)); ?>
<?php echo $form->error($model,'base_station_num'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
控制器
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BaseStation']))
{
$model->attributes=$_POST['BaseStation'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
答案 0 :(得分:1)
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BaseStation']))
{
$model->attributes=$_POST['BaseStation'];
$model->c_id = $model->c_id;
$model->base_station_num = $model->base_station_num;
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
试试这个