From d0c5ef832a054face4e8ccf41907ec71d79b1b48 Mon Sep 17 00:00:00 2001 From: Warren Moore Date: Wed, 9 Jul 2014 17:08:49 +0100 Subject: [PATCH] Add saveWithoutHydrating method --- src/LaravelBook/Ardent/Ardent.php | 42 ++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index e906e68..b9558f9 100755 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -499,12 +499,13 @@ protected static function makeValidator($data, $rules, $customMessages) { /** * Validate the model instance * - * @param array $rules Validation rules - * @param array $customMessages Custom error messages + * @param array $rules Validation rules + * @param array $customMessages Custom error messages + * @param bool $skipEntityHydration Override entity hydration * @return bool * @throws InvalidModelException */ - public function validate(array $rules = array(), array $customMessages = array()) { + public function validate(array $rules = array(), array $customMessages = array(), $skipEntityHydration = false) { if ($this->fireModelEvent('validating') === false) { if ($this->throwOnValidation) { throw new InvalidModelException($this); @@ -524,9 +525,10 @@ public function validate(array $rules = array(), array $customMessages = array() if (empty($rules)) { $success = true; } else { - $customMessages = (empty($customMessages))? static::$customMessages : $customMessages; - - if ($this->forceEntityHydrationFromInput || (empty($this->attributes) && $this->autoHydrateEntityFromInput)) { + $customMessages = (empty($customMessages))? static::$customMessages : $customMessages; + + $canHydrate = $this->forceEntityHydrationFromInput || (empty($this->attributes) && $this->autoHydrateEntityFromInput); + if ($canHydrate && !$skipEntityHydration) { $this->fill(Input::all()); } @@ -570,6 +572,7 @@ public function validate(array $rules = array(), array $customMessages = array() * @param Closure $beforeSave * @param Closure $afterSave * @param bool $force Forces saving invalid data. + * @param bool $skipEntityHydration * @return bool * @see Ardent::save() @@ -580,7 +583,8 @@ protected function internalSave(array $rules = array(), array $options = array(), Closure $beforeSave = null, Closure $afterSave = null, - $force = false + $force = false, + $skipEntityHydration = false ) { if ($beforeSave) { self::saving($beforeSave); @@ -589,7 +593,7 @@ protected function internalSave(array $rules = array(), self::saved($afterSave); } - $valid = $this->validate($rules, $customMessages); + $valid = $this->validate($rules, $customMessages, $skipEntityHydration); if ($force || $valid) { return $this->performSave($options); @@ -638,7 +642,27 @@ public function forceSave(array $rules = array(), ) { return $this->internalSave($rules, $customMessages, $options, $beforeSave, $afterSave, true); } - + + /** + * Save the model to the database without hydrating from input. + * + * @param array $rules + * @param array $customMessages + * @param array $options + * @param Closure $beforeSave + * @param Closure $afterSave + * + * @return bool + * @see Ardent::save() + */ + public function saveWithoutHydrating(array $rules = array(), + array $customMessages = array(), + array $options = array(), + Closure $beforeSave = null, + Closure $afterSave = null + ) { + return $this->internalSave($rules, $customMessages, $options, $beforeSave, $afterSave, false, true); + } /** * Add the basic purge filters