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

Commit

Permalink
Using Eloquent::fill() to hydrate model
Browse files Browse the repository at this point in the history
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
  • Loading branch information
igorsantos07 committed Aug 2, 2013
1 parent 35ccdc2 commit f400245
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
8 changes: 1 addition & 7 deletions src/LaravelBook/Ardent/Ardent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f400245

Please sign in to comment.