Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Add saveWithoutHydrating method #224

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/LaravelBook/Ardent/Ardent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be a better way to do this

if ($canHydrate && !$skipEntityHydration) {
$this->fill(Input::all());
}

Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down