From f400245f6a9b7dce7e917c7bf5339e9f6ff17aad Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Thu, 1 Aug 2013 23:37:27 -0300 Subject: [PATCH] Using `Eloquent::fill()` to hydrate model The old method relied on validation rules, meaning that properties that were not validated would not be populated when the model is configured to be automatically hydrated. Documentation was changed to reflect this behavior, and also to explicitly explain the differences between forced and automatic hydration. This references #77 and #78 and closes #51 --- README.md | 9 +++++---- src/LaravelBook/Ardent/Ardent.php | 8 +------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 54089d1..b5cb1e1 100644 --- a/README.md +++ b/README.md @@ -233,14 +233,15 @@ $user->save(); That's it! All we've done is remove the boring stuff. -Believe it or not, the code above performs essentially the same task as its older, albeit rather verbose sibling. Ardent populates the model object with attributes from user submitted form data (it uses the Laravel `Input::all()` method internally). No more hair-pulling trying to find out which Eloquent property you've forgotten to populate. Let Ardent take care of the boring stuff, while you get on with the fun stuffs! +Believe it or not, the code above performs essentially the same task as its older, albeit rather verbose sibling. Ardent populates the model object with attributes from user submitted form data. No more hair-pulling trying to find out which Eloquent property you've forgotten to populate. Let Ardent take care of the boring stuff, while you get on with the fun stuffs! +It follows the same [mass assignment rules](http://four.laravel.com/docs/eloquent#mass-assignment) internally, depending on the `$fillable`/`$guarded` properties. -To enable the auto-hydration feature, simply set the `$autoHydrateEntityFromInput` instance variable to `true` in your model class. However, to prevent filling pre-existent properties, if you want auto-hydration also for update scenarios, you should also use `$forceEntityHydrationFromInput`: +To enable the auto-hydration feature, simply set the `$autoHydrateEntityFromInput` instance variable to `true` in your model class. However, to prevent filling pre-existent properties, if you want auto-hydration also for update scenarios, you should use instead `$forceEntityHydrationFromInput`: ```php class User extends \LaravelBook\Ardent\Ardent { - public $autoHydrateEntityFromInput = true; // hydrates on new entries - public $forceEntityHydrationFromInput = true; // hydrates on updates + public $autoHydrateEntityFromInput = true; // hydrates on new entries' validation + public $forceEntityHydrationFromInput = true; // hydrates whenever validation is called } ``` diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index 958f786..70bd61a 100644 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -501,13 +501,7 @@ public function validate(array $rules = array(), array $customMessages = array() $customMessages = (empty($customMessages))? static::$customMessages : $customMessages; if ($this->forceEntityHydrationFromInput || (empty($this->attributes) && $this->autoHydrateEntityFromInput)) { - // pluck only the fields which are defined in the validation rule-set - $attributes = array_intersect_key(Input::all(), $rules); - - //Set each given attribute on the model - foreach ($attributes as $key => $value) { - $this->setAttribute($key, $value); - } + $this->fill(Input::all()); } $data = $this->getAttributes(); // the data under validation