From 74137c6be12565dc7f4a33560f22b464a7d17592 Mon Sep 17 00:00:00 2001 From: warrenca Date: Thu, 25 Jul 2013 22:25:52 +0800 Subject: [PATCH] Adding doc on how to auto hydrate fields when updating a record. --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 63a81a0..618d9ed 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,38 @@ class User extends \LaravelBook\Ardent\Ardent { public $autoHydrateEntityFromInput = true; } ``` +####Using $autoHydrateEntityFromInput on model "update" + +Even if `$autoHydrateEntityFromInput` is already set to `true`, it will not work if you are updating a record. + +To solve this problem, below is an example on how to change the `username` field of the Users table. + +Using plain Laravel model: + +```php +$user = User::find(1); +$user->username = Input::get('username'); +$user->save(); +``` + +Using model extended with Ardent: + +```php +$user = User::find(1); +$user->forceEntityHydrationFromInput = true; // add this line to force hydration +$user->save(); +``` + +If we are going to view our Users table, the `username` with `id` equal to `1` will be updated with the value of `Input::get('username')`. Try to set `$user->forceEntityHydrationFromInput` to false and you will see that no updates will happen. + +The `Ardent->forceEntityHydrationFromInput` can also be set in your model class: + +```php +class User extends \LaravelBook\Ardent\Ardent { + public $autoHydrateEntityFromInput = true; + public $forceEntityHydrationFromInput = true; +} +``` ## Model Hooks (since [2.0](https://github.com/laravelbook/ardent/tree/v2.0.0))