/** * Edits the model, runs validation and renders the edit form. * * @throws CHttpException * @throws Exception */ 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)); }
public function save($runValidation = true, $attributes = null, $allow_overriding = false) { if (isset($_SERVER['REMOTE_ADDR'])) { if (!($ipaddr = AuditIPAddr::model()->find('name=?', array($_SERVER['REMOTE_ADDR'])))) { $ipaddr = new AuditIPAddr(); $ipaddr->name = $_SERVER['REMOTE_ADDR']; if (!$ipaddr->save()) { throw new Exception("Unable to save audit IP address: " . print_r($ipaddr->getErrors(), true)); } } if (isset($_SERVER['HTTP_USER_AGENT'])) { if (!($useragent = AuditUseragent::model()->find('name=?', array($_SERVER['HTTP_USER_AGENT'])))) { $useragent = new AuditUseragent(); $useragent->name = $_SERVER['HTTP_USER_AGENT']; if (!$useragent->save()) { throw new Exception("Unable to save user agent: " . print_r($useragent->getErrors(), true)); } } $this->useragent_id = $useragent->id; } if (!($server = AuditServer::model()->find('name=?', array($_SERVER['SERVER_NAME'])))) { $server = new AuditServer(); $server->name = $_SERVER['SERVER_NAME']; if (!$server->save()) { throw new Exception("Unable to save server: " . print_r($server->getErrors(), true)); } } $this->ipaddr_id = $ipaddr->id; $this->server_id = $server->id; $this->request_uri = $_SERVER['REQUEST_URI']; if ($this->user) { $this->site_id = Yii::app()->session['selected_site_id']; $this->firm_id = Yii::app()->session['selected_firm_id']; } } return parent::save($runValidation, $attributes, $allow_overriding); }
protected function saveModel(\BaseActiveRecord $model) { if (!$model->save()) { throw new ValidationFailure("Validation failure on " . get_class($model), $model->errors); } }
public function save($runValidation = true, $attributes = null, $allow_overriding = false) { if ($this->version_id) { throw new Exception('save() should not be called on versiond model instances.'); } return parent::save($runValidation, $attributes, $allow_overriding); }