From d36660da78c965e597025904cbfc2ab29416b033 Mon Sep 17 00:00:00 2001 From: nightowl77 Date: Thu, 22 Aug 2013 10:11:54 +0200 Subject: [PATCH] Added changes as per Sylph's post. Now adds support for custom primary keys, auto population of table names --- README.md | 14 ++++++-------- src/LaravelBook/Ardent/Ardent.php | 4 +++- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5ac1738..9093a23 100644 --- a/README.md +++ b/README.md @@ -141,8 +141,8 @@ class User extends \LaravelBook\Ardent\Ardent { public static $rules = array( 'name' => 'required|between:4,16', 'email' => 'required|email', - 'password' => 'required|alpha_num|between:4,8|confirmed', - 'password_confirmation' => 'required|alpha_num|between:4,8', + 'password' => 'required|alpha_num|between:4,8|confirmed', + 'password_confirmation' => 'required|alpha_num|between:4,8', ); } ``` @@ -359,9 +359,8 @@ function __construct() { } ``` - - -## Automatically Transform Secure-Text Attributes + +## Updates with Unique Rules Suppose you have an attribute named `password` in your model class, but don't want to store the plain-text version in the database. The pragmatic thing to do would be to store the hash of the original content. Worry not, Ardent is fully capable of transmogrifying any number of secure fields automatically for you! @@ -377,9 +376,8 @@ class User extends \LaravelBook\Ardent\Ardent { Ardent will automatically replace the plain-text password attribute with secure hash checksum and save it to database. It uses the Laravel `Hash::make()` method internally to generate hash. - -## Updates with Unique Rules - + +## Automatically Transform Secure-Text Attributes Ardent can assist you with unique updates. According to the Lavavel Documentation, when you update (and therefore validate) a field with a unique rule, you have to pass in the unique ID of the record you are updating. Without passing this ID, validation will fail because Laravel's Validator will think this record is a duplicate. From the Laravel Documentation: diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index 2a89ba7..dbec390 100644 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -770,6 +770,7 @@ protected function buildUniqueExclusionRules(array $rules = array()) { // if the 3rd param was set, do not overwrite it if (!is_numeric(@$params[2])) $params[2] = $this->id; + $rule = implode(',', $params); } } @@ -779,7 +780,8 @@ protected function buildUniqueExclusionRules(array $rules = array()) { } /** - * Update a model already saved in the database. + * Update a model, but filter uniques first to ensure a unique validation rule + * does not fire * * @param array $rules * @param array $customMessages