php-无法在yii中保存模型

我在下拉菜单添加一个用于更新yii中的模型属性的ajax,但似乎模型没有保存在数据库中,并且在我检查模型时没有验证错误

风景

<?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'
                )
            )); ?>

控制器

public function actionbedUpdate()
{
    if(!isset($_POST['bed_id']))
        throw new CHttpException(400, 'Bad Request');

    if(!isset($_POST['bed']))
        throw new CHttpException(400, 'Bad Request');

    $model = bed::model()->findByPk($_POST['bed_id']);

    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');

    $model->attributes = $_POST['bed'];

    $model->save();

    //    throw new CHttpException(422, 'Saving Error');
}

示范规则

/**
 * @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'),
    );
}

解决方法:

我认为您忘记为帖子请求添加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'
                )
            )); ?>

相关文章

1、将Yii2.0advanced版中应用主体frontend或backend应用复制...
Yii2restfulAPI文档一、配置模块:1.Config/main.php:  2...
Yii在framework/i18n/data/%lang%.php文件中有很多翻译.这...
在Yii2中,官方的页面多语言解决方案有两个:方案1,使用Yii...
Yii2.0对数据库查询的一些简单的操作1234567891011121314151...
数据查询User::find()->all();此方法返回所有数据;User:...