首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii框架中的编辑操作

Yii框架中的编辑操作
EN

Stack Overflow用户
提问于 2015-06-01 13:27:38
回答 2查看 267关注 0票数 3

我正在开发一个允许用户举行视频会议的web应用程序。用户可以创建视频会议,我希望他们也能够编辑预定的视频会议,但我有困难实现这一点。请帮帮忙。

index.php视图中的编辑按钮

代码语言:javascript
复制
 $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")
    );

视频会议控制器actionUpdate

代码语言:javascript
复制
/**
 * 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,
    ));

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-01 21:45:14

经过一点故障排除后,我终于开始工作了。在视图中,我调用的actionUpdate方法如下:

代码语言:javascript
复制
        $html .=   CHtml::button('Edit', array('submit' => array('videoConference/update/'.$vc->id), "visible" =>  $ismoderator, 'role' => "button", "class" => "btn btn-info"));

刚换了控制器

代码语言:javascript
复制
$model->save() to $model->update()

而且效果很好。

票数 1
EN

Stack Overflow用户

发布于 2015-06-01 15:28:03

第一步是找出问题所在(前端/后端)。您需要在不使用ajax的情况下调用操作(只需使用param id的url )。试试我的版本:

代码语言:javascript
复制
/**
 * 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,
    ));
}

如果在服务器端所有工作正常(行已被更新),则检查请求参数、控制台、令牌等。问题将出现在前面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30574821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档