You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 16, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+87-47Lines changed: 87 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Update your packages with `composer update` or install with `composer install`.
22
22
23
23
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).
24
24
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))
26
26
27
27
If you're willing to use Ardent as a standalone ORM package you're invited to do so by using the
28
28
following configuration line in your project's boot/startup file (changing the properties according
@@ -48,9 +48,10 @@ to your database, obviously):
48
48
*[Effortless Validation with Ardent](#validation)
49
49
*[Retrieving Validation Errors](#errors)
50
50
*[Overriding Validation](#override)
51
-
*[Model hooks](#modelhooks)
52
51
*[Custom Validation Error Messages](#messages)
53
52
*[Custom Validation Rules](#rules)
53
+
*[Model hooks](#modelhooks)
54
+
*[Cleaner definition of relationships](#relations)
54
55
*[Automatically Hydrate Ardent Entities](#hydra)
55
56
*[Automatically Purge Redundant Form Data](#purge)
@@ -189,8 +190,61 @@ An array that is **not empty** will override the rules or custom error messages
189
190
190
191
> **Note:** the default value for `$rules` and `$customMessages` is empty `array()`; thus, if you pass an `array()` nothing will be overriden.
191
192
193
+
<aname="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
+
<aname="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
+
<aname="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:
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
+
192
246
<aname="modelhooks"></a>
193
-
## Model Hooks
247
+
## Model Hooks (since [2.0](https://github.com/laravelbook/ardent/tree/v2.0.0))
194
248
195
249
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.
196
250
@@ -219,7 +273,7 @@ class User extends \LaravelBook\Ardent\Ardent {
219
273
}
220
274
```
221
275
222
-
### Additionals beforeSave and afterSave
276
+
### Additionals beforeSave and afterSave (since 1.0)
223
277
224
278
`beforeSave` and `afterSave` can be included at run-time. Simply pass in closures with the model as argument to the `save()` (or `forceSave()`) method.
> **Note:** the closures should have one parameter as it will be passed a reference to the model being saved.
239
293
240
-
<aname="messages"></a>
241
-
## Custom Error Messages
294
+
<aname="relations"></a>
295
+
## Cleaner definition of relationships (since [2.0](https://github.com/laravelbook/ardent/tree/v2.0.0))
242
296
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:
244
300
245
301
```php
246
302
class User extends \LaravelBook\Ardent\Ardent {
247
-
public static $customMessages = array(
248
-
'required' => 'The :attribute field is required.',
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
-
<aname="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:
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
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).
0 commit comments