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

Commit 957c3a3

Browse files
committed
Including documentation about Ardent::$relationsData
1 parent 0644b22 commit 957c3a3

File tree

1 file changed

+87
-47
lines changed

1 file changed

+87
-47
lines changed

README.md

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Update your packages with `composer update` or install with `composer install`.
2222

2323
You can also add the package using `composer require laravelbook/ardent` and later specifying the version you want (for now, `dev-master` is your best bet).
2424

25-
### Usage outside of Laravel (since [ff41aae](https://github.com/laravelbook/ardent/commit/ff41aae645e38f21c0b5b9ee542a66ecc25f06a0))
25+
### Usage outside of Laravel (since [1.1](https://github.com/laravelbook/ardent/tree/v1.1.0))
2626

2727
If you're willing to use Ardent as a standalone ORM package you're invited to do so by using the
2828
following configuration line in your project's boot/startup file (changing the properties according
@@ -48,9 +48,10 @@ to your database, obviously):
4848
* [Effortless Validation with Ardent](#validation)
4949
* [Retrieving Validation Errors](#errors)
5050
* [Overriding Validation](#override)
51-
* [Model hooks](#modelhooks)
5251
* [Custom Validation Error Messages](#messages)
5352
* [Custom Validation Rules](#rules)
53+
* [Model hooks](#modelhooks)
54+
* [Cleaner definition of relationships](#relations)
5455
* [Automatically Hydrate Ardent Entities](#hydra)
5556
* [Automatically Purge Redundant Form Data](#purge)
5657
* [Automatically Transform Secure-Text Attributes](#secure)
@@ -189,8 +190,61 @@ An array that is **not empty** will override the rules or custom error messages
189190

190191
> **Note:** the default value for `$rules` and `$customMessages` is empty `array()`; thus, if you pass an `array()` nothing will be overriden.
191192
193+
<a name="messages"></a>
194+
## Custom Error Messages
195+
196+
Just like the Laravel Validator, Ardent lets you set custom error messages using the [same sytax](http://doc.laravelbook.com/validation/#custom-error-messages).
197+
198+
```php
199+
class User extends \LaravelBook\Ardent\Ardent {
200+
public static $customMessages = array(
201+
'required' => 'The :attribute field is required.',
202+
...
203+
);
204+
}
205+
```
206+
207+
<a name="rules"></a>
208+
## Custom Validation Rules
209+
210+
You can create custom validation rules the [same way](http://doc.laravelbook.com/validation/#custom-validation-rules) you would for the Laravel Validator.
211+
212+
<a name="hydra"></a>
213+
## Automatically Hydrate Ardent Entities
214+
215+
Ardent is capable of hydrating your entity model class from the form input submission automatically!
216+
217+
Let's see it action. Consider this snippet of code:
218+
219+
```php
220+
$user = new User;
221+
$user->name = Input::get('name');
222+
$user->email = Input::get('email');
223+
$user->password = Hash::make(Input::get('password'));
224+
$user->save();
225+
```
226+
227+
Let's invoke the *magick* of Ardent and rewrite the previous snippet:
228+
229+
```php
230+
$user = new User;
231+
$user->save();
232+
```
233+
234+
That's it! All we've done is remove the boring stuff.
235+
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!
237+
238+
To enable the auto-hydration feature, simply set the `$autoHydrateEntityFromInput` instance variable to `true` in your model class:
239+
240+
```php
241+
class User extends \LaravelBook\Ardent\Ardent {
242+
public $autoHydrateEntityFromInput = true;
243+
}
244+
```
245+
192246
<a name="modelhooks"></a>
193-
## Model Hooks
247+
## Model Hooks (since [2.0](https://github.com/laravelbook/ardent/tree/v2.0.0))
194248

195249
Ardent provides some syntatic sugar over Eloquent's model events: traditional model hooks. They are an easy way to hook up additional operations to different moments in your model life. They can be used to do additional clean-up work before deleting an entry, doing automatic fixes after validation occurs or updating related models after an update happens.
196250

@@ -219,7 +273,7 @@ class User extends \LaravelBook\Ardent\Ardent {
219273
}
220274
```
221275

222-
### Additionals beforeSave and afterSave
276+
### Additionals beforeSave and afterSave (since 1.0)
223277

224278
`beforeSave` and `afterSave` can be included at run-time. Simply pass in closures with the model as argument to the `save()` (or `forceSave()`) method.
225279

@@ -237,58 +291,44 @@ $user->save(array(), array(),
237291

238292
> **Note:** the closures should have one parameter as it will be passed a reference to the model being saved.
239293
240-
<a name="messages"></a>
241-
## Custom Error Messages
294+
<a name="relations"></a>
295+
## Cleaner definition of relationships (since [2.0](https://github.com/laravelbook/ardent/tree/v2.0.0))
242296

243-
Just like the Laravel Validator, Ardent lets you set custom error messages using the [same sytax](http://doc.laravelbook.com/validation/#custom-error-messages).
297+
Have you ever written an Eloquent model with a bunch of relations, just to notice how cluttered your class is, with all those one-liners that have almost the same content as the method name itself?
298+
299+
In Ardent you can cleanly define your relationships in an array with their information, and they will work just like if you had defined they in methods. Here's an example:
244300

245301
```php
246302
class User extends \LaravelBook\Ardent\Ardent {
247-
public static $customMessages = array(
248-
'required' => 'The :attribute field is required.',
249-
...
303+
public static $relationsData = array(
304+
'address' => array(self::HAS_ONE, 'Address'),
305+
'orders' => array(self::HAS_MANY, 'Order'),
306+
'groups' => array(self::BELONGS_TO_MANY, 'Group', 'table' => 'groups_have_users')
250307
);
251308
}
252-
```
253-
254-
<a name="rules"></a>
255-
## Custom Validation Rules
256-
257-
You can create custom validation rules the [same way](http://doc.laravelbook.com/validation/#custom-validation-rules) you would for the Laravel Validator.
258-
259-
<a name="hydra"></a>
260-
## Automatically Hydrate Ardent Entities
261-
262-
Ardent is capable of hydrating your entity model class from the form input submission automatically!
263-
264-
Let's see it action. Consider this snippet of code:
265309

266-
```php
267-
$user = new User;
268-
$user->name = Input::get('name');
269-
$user->email = Input::get('email');
270-
$user->password = Hash::make(Input::get('password'));
271-
$user->save();
272-
```
273-
274-
Let's invoke the *magick* of Ardent and rewrite the previous snippet:
275-
276-
```php
277-
$user = new User;
278-
$user->save();
310+
$user = User::find($id);
311+
echo "{$user->address->street}, {$user->address->city} - {$user->address->state}";
279312
```
280313

281-
That's it! All we've done is remove the boring stuff.
282-
283-
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!
284-
285-
To enable the auto-hydration feature, simply set the `$autoHydrateEntityFromInput` instance variable to `true` in your model class:
286-
287-
```php
288-
class User extends \LaravelBook\Ardent\Ardent {
289-
public $autoHydrateEntityFromInput = true;
290-
}
291-
```
314+
The array syntax is as follows:
315+
316+
- First indexed value: relation name, being one of
317+
[`hasOne`](http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_hasOne),
318+
[`hasMany`](http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_hasMany),
319+
[`belongsTo`](http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_belongsTo),
320+
[`belongsToMany`](http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_belongsToMany),
321+
[`morphTo`](http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_morphTo),
322+
[`morphOne`](http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_morphOne),
323+
[`morphMany`](http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_morphMany),
324+
or one of the related constants (`Ardent::HAS_MANY` or `Ardent::MORPH_ONE` for example).
325+
- Second indexed: class name, with complete namespace. The exception is `morphTo` relations, that take no additional argument.
326+
- named arguments, following the ones defined for the original Eloquent methods:
327+
- `foreignKey` [optional], valid for `hasOne`, `hasMany`, `belongsTo` and `belongsToMany`
328+
- `table` and `otherKey` [optional], valid for `belongsToMany`
329+
- `name`, `type` and `id`, used by `morphTo`, `morphOne` and `morphMany` (the last two requires `name` to be defined)
330+
331+
> **Note:** This feature was based on the easy [relations on Yii 1.1 ActiveRecord](http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship).
292332
293333
<a name="purge"></a>
294334
## Automatically Purge Redundant Form Data

0 commit comments

Comments
 (0)