BaseActiveRecord::save PHP Method

save() public method

public save ( boolean $runValidation = true, array $attributes = null, boolean $allow_overriding = false ) : boolean
$runValidation boolean
$attributes array
$allow_overriding boolean - if true allows created/modified user/date to be set and saved via the model (otherwise gets overriden)
return boolean
    public function save($runValidation = true, $attributes = null, $allow_overriding = false)
    {
        // Saving the model only if it is dirty / turn on/off with $this->save_only_if_dirty
        if ($this->save_only_if_dirty === true && $this->isModelDirty() === false) {
            return false;
        }
        $user_id = null;
        try {
            if (isset(Yii::app()->user)) {
                $user_id = Yii::app()->user->id;
            }
        } catch (Exception $e) {
        }
        if ($this->getIsNewRecord() || !isset($this->id)) {
            if (!$allow_overriding) {
                // Set creation properties
                if ($user_id === null) {
                    // Revert to the admin user
                    $this->created_user_id = 1;
                } else {
                    $this->created_user_id = $user_id;
                }
            }
            if (!$allow_overriding || $this->created_date == '1900-01-01 00:00:00') {
                $this->created_date = date('Y-m-d H:i:s');
            }
        }
        try {
            if (!$allow_overriding) {
                // Set the last_modified_user_id and last_modified_date fields
                if ($user_id === null) {
                    // Revert to the admin user
                    // need this try/catch block here to make older migrations pass with this hook in place
                    $this->last_modified_user_id = 1;
                } else {
                    $this->last_modified_user_id = $user_id;
                }
            }
            if (!$allow_overriding || $this->last_modified_date == '1900-01-01 00:00:00') {
                $this->last_modified_date = date('Y-m-d H:i:s');
            }
        } catch (Exception $e) {
        }
        return parent::save($runValidation, $attributes);
    }

Usage Example

示例#1
0
 /**
  * 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));
 }
All Usage Examples Of BaseActiveRecord::save