gpt4 book ai didi

php - 模型不保存在 yii 中

转载 作者:可可西里 更新时间:2023-11-01 01:01:43 27 4
gpt4 key购买 nike

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

View

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

Controller

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 token

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

关于php - 模型不保存在 yii 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23266771/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com