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

Commit f400245

Browse files
committed
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
1 parent 35ccdc2 commit f400245

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,15 @@ $user->save();
233233

234234
That's it! All we've done is remove the boring stuff.
235235

236-
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!
236+
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!
237+
It follows the same [mass assignment rules](http://four.laravel.com/docs/eloquent#mass-assignment) internally, depending on the `$fillable`/`$guarded` properties.
237238

238-
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`:
239+
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`:
239240

240241
```php
241242
class User extends \LaravelBook\Ardent\Ardent {
242-
public $autoHydrateEntityFromInput = true; // hydrates on new entries
243-
public $forceEntityHydrationFromInput = true; // hydrates on updates
243+
public $autoHydrateEntityFromInput = true; // hydrates on new entries' validation
244+
public $forceEntityHydrationFromInput = true; // hydrates whenever validation is called
244245
}
245246
```
246247

src/LaravelBook/Ardent/Ardent.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,7 @@ public function validate(array $rules = array(), array $customMessages = array()
501501
$customMessages = (empty($customMessages))? static::$customMessages : $customMessages;
502502

503503
if ($this->forceEntityHydrationFromInput || (empty($this->attributes) && $this->autoHydrateEntityFromInput)) {
504-
// pluck only the fields which are defined in the validation rule-set
505-
$attributes = array_intersect_key(Input::all(), $rules);
506-
507-
//Set each given attribute on the model
508-
foreach ($attributes as $key => $value) {
509-
$this->setAttribute($key, $value);
510-
}
504+
$this->fill(Input::all());
511505
}
512506

513507
$data = $this->getAttributes(); // the data under validation

0 commit comments

Comments
 (0)