weixin_33725722 2015-06-01 13:27 采纳率: 0%
浏览 30

在Yii框架中编辑动作

I am working on a web application that allow users to have video conferences. Users are allowed to create video conferences and I want them to be able to also edit the scheduled video conferences but I am having trouble implementing that. Please help.

Edit button in index.php view

 $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")
    );

Video conference Controller actionUpdate

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

}
  • 写回答

2条回答 默认 最新

  • weixin_33697898 2015-06-01 15:28
    关注

    The first step is to find where is problem(frontend / backend). You need call action without ajax(just from url with param id). Try my version:

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

    If on server side all working fine(row was updated) then check request params, console, tokens etc. Problem will be on frontend.

    评论

报告相同问题?