Php 模型不保存在yii中
我已经做了一个下拉式ajax来更新yii中的模型属性,但似乎模型没有保存在数据库中,并且在检查模型时没有验证错误 景色Php 模型不保存在yii中,php,yii,Php,Yii,我已经做了一个下拉式ajax来更新yii中的模型属性,但似乎模型没有保存在数据库中,并且在检查模型时没有验证错误 景色 <?php echo CHtml::dropDownList('roomType', $bed->room_type, SiteBed::roomTypes(), array('class' => 'room-types', 'ajax' => array( 'type' =>
<?php echo CHtml::dropDownList('roomType', $bed->room_type, SiteBed::roomTypes(), array('class' => 'room-types',
'ajax' => array(
'type' => 'POST',
'url' => Yii::app()->createUrl("admission/admit/bedUpdate", 'ajax' => TRUE)),
'data' => array('Bed[room_type]' => 'js:this.value', 'bed_id' => $bed->bed_id),
'update' => '#Bed_room_type'
)
)); ?>
模型规则
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, status, price, room_type, house_id', 'required'),
array('status, room_type, house_id', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>150),
array('description', 'length', 'max'=>500),
array('price', 'length', 'max'=>10),
array('date_created, date_modified', 'length', 'max'=>14),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('bed_id, name, description, status, price, room_type, house_id, date_created, date_modified', 'safe', 'on'=>'search'),
);
}
我想你忘了为你的post请求添加csrf令牌
<?php echo CHtml::dropDownList('roomType', $bed->room_type, SiteBed::roomTypes(), array('class' => 'room-types',
'ajax' => array(
'type' => 'POST',
'url' => Yii::app()->createUrl("admission/admit/bedUpdate", 'ajax' => TRUE)),
'data' => array(
'Bed[room_type]' => 'js:this.value',
'bed_id' => $bed->bed_id,
'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken
),
'update' => '#Bed_room_type'
)
)); ?>
我发现yii生成的jQuery代码是问题所在,我忘了在这里提到我创建了多个CHtml::dropDownList('roomType'),因为我在这里使用了GridView,问题是如果dropDownList被生成了多次,yii将使用下拉元素的id,在本例中是roomType。我想yii应该使用元素类(房间类型)来代替。感谢您的回复向我们展示您的模型规则,并执行如果(!$model->save()){print\r($model->getErrors());}
查看错误。您好,我已经执行了$model->getErrors(),但没有发现任何错误。如果我在没有ajax的情况下保存它,它工作得很好,但是如果我使用ajax保存它,它不会尝试跳过验证,使用$model->save(false);是的,你可以跳过@Abudayah提到的验证,但是检查你模型中的规则。
<?php echo CHtml::dropDownList('roomType', $bed->room_type, SiteBed::roomTypes(), array('class' => 'room-types',
'ajax' => array(
'type' => 'POST',
'url' => Yii::app()->createUrl("admission/admit/bedUpdate", 'ajax' => TRUE)),
'data' => array(
'Bed[room_type]' => 'js:this.value',
'bed_id' => $bed->bed_id,
'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken
),
'update' => '#Bed_room_type'
)
)); ?>