Admin::editModel PHP Method

editModel() public method

Edits the model, runs validation and renders the edit form.
public editModel ( )
    public function editModel()
    {
        $this->assetManager->registerScriptFile('js/oeadmin/edit.js');
        $errors = array();
        if (Yii::app()->request->isPostRequest) {
            $post = Yii::app()->request->getPost($this->modelName);
            if (empty($post)) {
                $this->modelName = str_replace('\\', '_', $this->modelName);
                $post = $_POST[$this->modelName];
            }
            if (array_key_exists('id', $post) && $post['id']) {
                $this->model->attributes = $post;
            } else {
                $this->model = new $this->modelName();
                $this->model->attributes = $post;
            }
            if (!$this->model->validate()) {
                $errors = $this->model->getErrors();
            } else {
                if (!$this->model->save()) {
                    throw new CHttpException(500, 'Unable to save ' . $this->modelName . ': ' . print_r($this->model->getErrors(), true));
                }
                $this->audit('edit', $this->model->id);
                $return = '/' . $this->controller->uniqueid . '/list';
                if (Yii::app()->request->getPost('returnUriEdit')) {
                    $return = urldecode(Yii::app()->request->getPost('returnUriEdit'));
                }
                $this->controller->redirect($return);
            }
        } else {
            $defaults = Yii::app()->request->getParam('default', array());
            foreach ($defaults as $key => $defaultValue) {
                if ($this->model->hasAttribute($key)) {
                    $this->model->{$key} = $defaultValue;
                }
            }
        }
        $this->render($this->editTemplate, array('admin' => $this, 'errors' => $errors));
    }

Usage Example

 /**
  * Edits or adds a Procedure
  *
  * @param bool|int $id
  * @throws CHttpException
  */
 public function actionEdit($id = false)
 {
     $admin = new Admin(Complication::model(), $this);
     if ($id) {
         $admin->setModelId($id);
     }
     $admin->setEditFields(array('name' => 'text', 'active' => 'checkbox'));
     $admin->editModel();
 }
All Usage Examples Of Admin::editModel