Invoked before a model is saved. Return false to abort the operation.

@return bool
16807
public function preSave()
    {
        if (!isset($this->revisionEnabled) || $this->revisionEnabled) {
            // if there's no revisionEnabled. Or if there is, if it's true

            $this->originalData = $this->original;
            $this->updatedData  = $this->attributes;

            // we can only safely compare basic items,
            // so for now we drop any object based items, like DateTime
            foreach ($this->updatedData as $key => $val) {
                if (gettype($val) == 'object' && ! method_exists($val, '__toString')) {
                    unset($this->originalData[$key]);
                    unset($this->updatedData[$key]);
                }
            }

            // the below is ugly, for sure, but it's required so we can save the standard model
            // then use the keep / dontkeep values for later, in the isRevisionable method
            $this->dontKeep = isset($this->dontKeepRevisionOf) ?
                array_merge($this->dontKeepRevisionOf, $this->dontKeep)
                : $this->dontKeep;

            $this->doKeep = isset($this->keepRevisionOf) ?
                array_merge($this->keepRevisionOf, $this->doKeep)
                : $this->doKeep;

            unset($this->attributes['dontKeepRevisionOf']);
            unset($this->attributes['keepRevisionOf']);

            $this->dirtyData = $this->getDirty();
            $this->updating = $this->exists;
        }
    }
595742
protected function beforeSave() 
	{
		$module = strtolower(Yii::app()->controller->module->id);
		$controller = strtolower(Yii::app()->controller->id);
		$action = strtolower(Yii::app()->controller->action->id);

		$location = $module.' '.$controller;
		
		if(parent::beforeSave()) {
			if($this->isNewRecord || (!$this->isNewRecord && !$this->name)) {
				$name=new SourceMessage;
				$name->message = $this->name_i;
				$name->location = $location.'_title';
				if($name->save())
					$this->name = $name->id;

				$this->slug = $this->urlTitle($this->name_i);
				
			} else {
				$name = SourceMessage::model()->findByPk($this->name);
				$name->message = $this->name_i;
				$name->save();
			}

			if($this->isNewRecord || (!$this->isNewRecord && !$this->desc)) {
				$desc=new SourceMessage;
				$desc->message = $this->desc_i;
				$desc->location = $location.'_desc';
				if($desc->save())
					$this->desc = $desc->id;
				
			} else {
				$desc = SourceMessage::model()->findByPk($this->desc);
				$desc->message = $this->desc_i;
				$desc->save();
			}

			if($action != 'publish') {
				$this->cat_code = $this->urlTitle($this->name_i);
				//Banner Size
				$this->banner_size = serialize($this->banner_size);
			}
		}
		return true;
	}
1012565
public function beforeSave()
    {
        $value = $this->getValue();
        if (is_array($value)) {
            unset($value['__empty']);
        }
        $this->setValue($value);

        return parent::beforeSave();
    }
46747
public function beforeSave(Model $model, $options = array()) {
        $alias = $model->alias;

        if (empty($model->data[$alias])) {
            return true;
        }

        // Loop through the data and upload the file
        foreach ($model->data[$alias] as $field => $file) {
            if (empty($this->settings[$alias][$field])) {
                continue;
            }

            // Gather attachment settings
            $attachment = $this->_settingsCallback($model, $this->settings[$alias][$field]);
            $data = array();

            // Initialize Transit
            $transit = new Transit($file);
            $transit->setDirectory($attachment['tempDir']);

            $this->_uploads[$alias] = $transit;

            // Set transformers and transporter
            $this->_addTransformers($model, $transit, $attachment);
            $this->_setTransporter($model, $transit, $attachment);

            // Attempt upload or import
            try {
                $overwrite = $attachment['overwrite'];
                $response = null;

                // File upload
                if (is_array($file)) {
                    $response = $transit->upload($overwrite);

                // Remote import
                } else if (preg_match('/^http/i', $file)) {
                    $response = $transit->importFromRemote($overwrite, $attachment['curl']);

                // Local import
                } else if (file_exists($file)) {
                    $response = $transit->importFromLocal($overwrite);

                // Stream import
                } else if (!empty($file)) {
                    $response = $transit->importFromStream($overwrite);
                }

                // Successful upload or import
                if ($response) {
                    $dbColumnMap = array($attachment['dbColumn']);
                    $transportConfig = array($this->_prepareTransport($attachment));

                    // Rename and move file
                    $data[$attachment['dbColumn']] = $this->_renameAndMove($model, $transit->getOriginalFile(), $attachment);

                    // Fetch exif data before transforming
                    $metaData = array();

                    foreach ($transit->getOriginalFile()->toArray() as $key => $value) {
                        if (substr($key, 0, 4) === 'exif' && $value) {
                            $metaData[$key] = $value;
                        }
                    }

                    // Transform the files and save their path
                    if ($attachment['transforms']) {
                        $transit->transform();

                        $transformedFiles = $transit->getTransformedFiles();
                        $count = 0;

                        foreach ($attachment['transforms'] as $transform) {
                            if ($transform['self']) {
                                $tempFile = $transit->getOriginalFile();
                                $dbColumnMap[0] = $transform['dbColumn'];

                            } else {
                                $tempFile = $transformedFiles[$count];
                                $dbColumnMap[] = $transform['dbColumn'];
                                $count++;

                                $transportConfig[] = $this->_prepareTransport($transform);
                            }

                            $data[$transform['dbColumn']] = $this->_renameAndMove($model, $tempFile, $transform);
                        }
                    }

                    $metaData = array_merge($transit->getOriginalFile()->toArray(), $metaData);

                    // Transport the files and save their remote path
                    if ($attachment['transport']) {
                        if ($transportedFiles = $transit->transport($transportConfig)) {
                            foreach ($transportedFiles as $i => $transportedFile) {
                                $data[$dbColumnMap[$i]] = $transportedFile;
                            }
                        }
                    }
                }

            // Trigger form errors if validation fails
            } catch (ValidationException $e) {
                $dbColumns = array_merge(array($attachment['dbColumn']), array_keys($attachment['transforms']));

                foreach ($dbColumns as $dbCol) {
                    unset($model->data[$alias][$dbCol]);
                }

                // Allow empty uploads
                if ($attachment['allowEmpty']) {
                    continue;
                }

                // Invalidate and stop
                $model->invalidate($field, __d('uploader', $e->getMessage()));

                if ($attachment['stopSave']) {
                    return false;
                }

            // Log exceptions that shouldn't be shown to the client
            } catch (Exception $e) {
                $model->invalidate($field, __d('uploader', $e->getMessage()));

                $this->log($e->getMessage(), LOG_DEBUG);

                // Rollback the files since it threw errors
                $transit->rollback();

                if ($attachment['stopSave']) {
                    return false;
                }
            }

            // Save file meta data
            $cleanup = $data;

            if ($attachment['metaColumns'] && $data && !empty($metaData)) {
                foreach ($attachment['metaColumns'] as $method => $column) {
                    if (isset($metaData[$method]) && $column) {
                        $data[$column] = $metaData[$method];
                    }
                }
            }

            // Merge upload data with model data
            if ($data) {
                $model->data[$alias] = $data + $model->data[$alias];
            }

            // Keep it in a loop, so it will delete all files
            // If we are doing an update, delete the previous files that are being replaced
            if ($model->id && $cleanup) {
                $this->_cleanupOldFiles($model, $cleanup);
            }
        }

        return true;
    }
764493
public function delete()
    {
        if (is_null($this->primaryKey) ||  is_null($this->getKey())) {
            throw new RuntimeException("No primary key defined on model.");
        }

        if ($this->exists) {
            if ($this->fireModelEvent('deleting') === false) {
                return false;
            }

            if (! $this->performDeleteOnModel()) {
                return false;
            }

            $this->exists = false;

            // Once the model has been deleted, we will fire off the deleted event so that
            // the developers may hook into post-delete operations. We will then return
            // a boolean true which indicated the success of the delete operation on the API.
            $this->fireModelEvent('deleted', true);

            return true;
        }
    }
929072
protected function preSave()
    {
        parent::preSave();

        if (password_needs_rehash($this->token, PASSWORD_DEFAULT)) {
            $this->token = password_hash($this->token, PASSWORD_DEFAULT);
        }
        $this->setCreated('now');
        $this->setLastModified('now');

        return true;
    }
1189990
public function beforeValidate()
    {
        $this->fileName = $this->owner->getAttribute($this->fileField);
        if (!is_string($this->fileName)) {
            $this->fileName = @$this->owner->oldAttributes[$this->fileField];
        }
        if (
            (!$files = UploadedFile::getInstance($this->owner, $this->fileField))
            && (!$files = UploadedFile::getInstances($this->owner, $this->fileField))     
        ) {
            return true;
        }
        return $this->owner->setAttribute($this->fileField, $files);
    }
149745
public function modelAction() {
        $name = ucfirst(Inflector::camelize($this->getFirstParamOrAsk('Enter model name')));
        $path = DC::getEnvironment()->getUserClassesRoot() . 'db/';
        $mo   = ModelOperator::getInstance($path);
        if ($mo->getModelStructure($name)) {
            $this->warning('model ' . $name . ' is already exists', '~ model exists, skipping:');
            return true;
        }
        $mo->generateBasicStructure($name);
        $mo->saveModelStructure($name);
        $this->notify($path . 'structure/' . $name . '.yml', '+ model created');
    }
363203
public function save()
    {
        event(new BeforeSave($this));

        $this->beforeSave();

        $this->doSave();

        $this->afterSave();

        event(new AfterSave($this));
    }
606182
function beforeSave( Model $Model, $options = array() ){
	  	if ( !array_key_exists('Media', $Model->belongsTo) ) {
		  	$Model->bindModel(
		        array('belongsTo' => array(
		                'Media' => array(
		                    'className' => 'Risto.Media'
		                )
		            )
		        )
		    );
	  	}

	    if( isset($Model->data[$Model->alias][$this->_form_field_name]) 
	    	&& isset( $Model->data[$Model->alias][$this->_form_field_name]['error'] )
	    	&& $Model->data[$Model->alias][$this->_form_field_name]['error'] == 0
	    	){
	      $data = array('Media' => $Model->data[$Model->alias][$this->_form_field_name]);
	      $data['Media']['file'] = file_get_contents($Model->data[$Model->alias][$this->_form_field_name]['tmp_name']);
	      $data['Media']['model'] = $Model->name;
	      $Model->Media->create();
	      if ( !$Model->Media->save($data)) {
	      		// error
	      		$Model->validationErrors[$this->_form_field_name] = $Model->Media->validationErrors;
	      		return false;
	      }
      	  $Model->data[$Model->alias][$this->_form_fk] = $Model->Media->id;
      	  
	    }

	    return true;
	  }
519970
public function beforeSave(Model $model, $options = array()) {
        $settings = $this->settings[$model->alias];

        if ($model->data[$model->alias]) {
            foreach ($model->data[$model->alias] as $key => &$value) {
                if (isset($settings[$key])) {
                    $filters = $settings[$key];

                    // Strip tags
                    if (isset($filters['strip']) && $filters['strip']['filter']) {
                        $value = strip_tags($value, (string) $filters['strip']['allowed']);
                    }

                    // HTML escape
                    if (isset($filters['html']) && $filters['html']['filter']) {
                        $value = Sanitize::html($value, array(
                            'quotes' => $filters['html']['flags'],
                            'charset' => $filters['html']['encoding'],
                            'double' => $filters['html']['double']
                        ));
                    }

                    // Paranoid
                    if (isset($filters['paranoid']) && $filters['paranoid']['filter']) {
                        $value = Sanitize::paranoid($value, (array) $filters['paranoid']['allowed']);
                    }

                    // SQL escape
                    if (isset($filters['escape']) && $filters['escape']['filter']) {
                        $value = Sanitize::escape($value, $model->useDbConfig);
                    }
                }
            }
        }

        return true;
    }
544152
protected function beforeValidate() 
	{
		if(parent::beforeValidate()) {
			if($this->isNewRecord) {
				if($this->actived == 1)
					$this->orders = count(self::getPlugin(1, null, 'data')) + 1;
				else
					$this->orders = 0;
				
				$this->creation_id = !Yii::app()->user->isGuest ? Yii::app()->user->id : null;
			} else
				$this->modified_id = !Yii::app()->user->isGuest ? Yii::app()->user->id : null;
		}
		return true;
	}
188783
public function save(Model $model)
    {
        $this->setForeignAttributesForCreate($model);

        return $model->save() ? $model : false;
    }
107337
public function beforeSave()
    {
        /* @var $model ActiveRecord */
        $model = $this->owner;
        foreach ($this->attributes as $attr => $options) {
            $this->ensureAttributes($attr, $options);
            if ($file = UploadedFile::getInstance($model, $attr)) {
                $this->createDirIfNotExists($attr);
                if (!$model->isNewRecord) {
                    $this->deleteFiles($attr);
                }
                $fileName = uniqid() . '.' . $file->extension;
                $model->{$attr} = $fileName;
                $file->saveAs($this->getSavePath($attr) . $fileName);
            } elseif (isset($model->oldAttributes[$attr])) {
                $model->{$attr} = $model->oldAttributes[$attr];
            }
        }
    }
406394
public function beforeDelete()
    {
        $handler = $this->get('metadata')->get('handler');
        if (class_exists($handler)) {
            $handler = new $handler();

            return $handler->beforeDelete($this);
        }

        return true;
    }
1198031
public function beforeSave(Model $Model, $options = array()) {
		if ($this->settings[$Model->alias]['auto'] === false) {
			return true;
		}
		if (!$Model->ShadowModel) {
			return true;
		}
		$Model->ShadowModel->create();
		if (!isset($Model->data[$Model->alias][$Model->primaryKey]) && !$Model->id) {
			return true;
		}

		$habtm = array();
		foreach ($Model->getAssociated('hasAndBelongsToMany') as $assocAlias) {
			if (isset($Model->ShadowModel->_schema[$assocAlias])) {
				$habtm[] = $assocAlias;
			}
		}
		$this->_oldData[$Model->alias] = $Model->find('first', array(
				'contain' => $habtm, 'conditions' => array($Model->alias . '.' . $Model->primaryKey => $Model->id)));

		return true;
	}
180552
public function beforeSave()
    {
        $this->getRecord()->active = 0;
        $this->getRecord()->cmsAuthIdReply = \App\Registry::$auth->getId();
        return true;
    }