Yii 框架中的编辑操作
Edit action in Yii framework
我正在开发一个允许用户进行视频会议的网络应用程序。允许用户创建视频会议,我希望他们也能够编辑预定的视频会议,但我在实现时遇到了麻烦。请帮忙
index.php 视图中的编辑按钮
$html .= CHtml::ajaxLink('Edit',
Yii::app()->createAbsoluteUrl('videoConference/update/'.$vc->id),
array(
'type'=>'post',
'data' => array('id' =>$vc->id,'type'=>'update'),
),
array( "visible" => $ismoderator, 'role' => "button", "class" => "btn btn-info")
);
视频会议控制器动作更新
/**
* 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);
if (isset($_POST['VideoConference'])) {
$model->attributes = $_POST['VideoConference'];
if ($model->save())
$this->redirect(array('view', 'id' => $model->id));
}
$this->render('edit', array(
'model' => $model,
));
}
第一步是找出问题所在(前端/后端)。您需要没有 ajax 的呼叫操作(仅来自 url 和参数 ID)。试试我的版本:
/**
* 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);
if ($model == null) {
throw new CHttpException(404, 'Model not exist.');
}
//if (isset($_POST['VideoConference'])) {
//$model->attributes = $_POST['VideoConference'];
$model->attributes = array('your_attr' => 'val', /* etc... */);
// or try to set 1 attribute $model->yourAttr = 'test';
if ($model->validate()) {
$model->update(); //better use update(), not save() for updating.
$this->redirect(array('view', 'id' => $model->id));
} else {
//check errors of validation
var_dump($model->getErrors());
die();
}
//}
$this->render('edit', array(
'model' => $model,
));
}
如果服务器端一切正常(行已更新),则检查请求参数、控制台、令牌等。问题将出现在前端。
经过一些故障排除后,我终于让它工作了。
在视图中,我这样调用 actionUpdate 方法:
$html .= CHtml::button('Edit', array('submit' => array('videoConference/update/'.$vc->id), "visible" => $ismoderator, 'role' => "button", "class" => "btn btn-info"));
刚换的控制器
$model->save() to $model->update()
而且效果很好。
我正在开发一个允许用户进行视频会议的网络应用程序。允许用户创建视频会议,我希望他们也能够编辑预定的视频会议,但我在实现时遇到了麻烦。请帮忙
index.php 视图中的编辑按钮
$html .= CHtml::ajaxLink('Edit',
Yii::app()->createAbsoluteUrl('videoConference/update/'.$vc->id),
array(
'type'=>'post',
'data' => array('id' =>$vc->id,'type'=>'update'),
),
array( "visible" => $ismoderator, 'role' => "button", "class" => "btn btn-info")
);
视频会议控制器动作更新
/**
* 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);
if (isset($_POST['VideoConference'])) {
$model->attributes = $_POST['VideoConference'];
if ($model->save())
$this->redirect(array('view', 'id' => $model->id));
}
$this->render('edit', array(
'model' => $model,
));
}
第一步是找出问题所在(前端/后端)。您需要没有 ajax 的呼叫操作(仅来自 url 和参数 ID)。试试我的版本:
/**
* 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);
if ($model == null) {
throw new CHttpException(404, 'Model not exist.');
}
//if (isset($_POST['VideoConference'])) {
//$model->attributes = $_POST['VideoConference'];
$model->attributes = array('your_attr' => 'val', /* etc... */);
// or try to set 1 attribute $model->yourAttr = 'test';
if ($model->validate()) {
$model->update(); //better use update(), not save() for updating.
$this->redirect(array('view', 'id' => $model->id));
} else {
//check errors of validation
var_dump($model->getErrors());
die();
}
//}
$this->render('edit', array(
'model' => $model,
));
}
如果服务器端一切正常(行已更新),则检查请求参数、控制台、令牌等。问题将出现在前端。
经过一些故障排除后,我终于让它工作了。 在视图中,我这样调用 actionUpdate 方法:
$html .= CHtml::button('Edit', array('submit' => array('videoConference/update/'.$vc->id), "visible" => $ismoderator, 'role' => "button", "class" => "btn btn-info"));
刚换的控制器
$model->save() to $model->update()
而且效果很好。