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

Commit e316250

Browse files
nightowl77igorsantos07
authored andcommitted
Added changes as per Sylph's post. Now adds support for custom primary keys, auto population of table names
1 parent 6151c4c commit e316250

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ to your database, obviously):
5555
* [Automatically Hydrate Ardent Entities](#hydra)
5656
* [Automatically Purge Redundant Form Data](#purge)
5757
* [Automatically Transform Secure-Text Attributes](#secure)
58+
* [Updates with Unique Rules] (#uniquerules)
5859

5960
<a name="start"></a>
6061
## Introduction
@@ -358,8 +359,8 @@ function __construct() {
358359
}
359360
```
360361

361-
<a name="secure"></a>
362-
## Automatically Transform Secure-Text Attributes
362+
<a name="uniquerules"></a>
363+
## Updates with Unique Rules
363364

364365
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!
365366

@@ -373,3 +374,39 @@ class User extends \LaravelBook\Ardent\Ardent {
373374
```
374375

375376
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.
377+
378+
379+
<a name="secure"></a>
380+
## Automatically Transform Secure-Text Attributes
381+
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.
382+
383+
From the Laravel Documentation:
384+
385+
```php
386+
'email' => 'unique:users,email,10'
387+
```
388+
389+
In the past, programmers had to manually manage the passing of the ID and changing of the ruleset to include the ID at runtime. Not so with Ardent. Simply set up your rules with `unique`, call function `updateUniques` and Ardent will take care of the rest.
390+
391+
#### Example:
392+
393+
In your extended model define your rules
394+
395+
```php
396+
public static $rules = array(
397+
'email' => 'required|email|unique',
398+
'password' => 'required|between:4,20|confirmed',
399+
'password_confirmation' => 'between:4,20',
400+
);
401+
```
402+
403+
In your controller, when you need to update, simply call
404+
405+
```php
406+
$model->updateUniques();
407+
```
408+
409+
If required, you can runtime pass rules to `updateUniques`, otherwise it will use the static rules provided by your model.
410+
411+
Note that in the above example of the rules, we did not tell the Validator which table or even which field to use as it is described in the Laravel Documentation (ie `unique:users,email,10`). Ardent is clever enough to figure it out. (Thank you to github user @Sylph)
412+

src/LaravelBook/Ardent/Ardent.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -759,23 +759,38 @@ protected function buildUniqueExclusionRules(array $rules = array()) {
759759
$ruleset = (is_string($ruleset))? explode('|', $ruleset) : $ruleset;
760760

761761
foreach ($ruleset as &$rule) {
762-
if (strpos($rule, 'unique') === 0) {
763-
$params = explode(',', $rule);
764-
765-
// Append field name if needed
766-
if (count($params) == 1) {
767-
$params[1] = $field;
768-
}
769-
770-
// if the 3rd param was set, do not overwrite it
771-
if (!is_numeric(@$params[2])) $params[2] = $this->id;
772-
773-
774-
$rule = implode(',', $params);
762+
if (strpos($rule, 'unique') === 0) {
763+
$params = explode(',', $rule);
764+
765+
$uniqueRules = array();
766+
767+
// Append table name if needed
768+
$table = explode(':', $params[0]);
769+
if (count($table) == 1)
770+
$uniqueRules[1] = $this->table;
771+
else
772+
$uniqueRules[1] = $table[1];
773+
774+
// Append field name if needed
775+
if (count($params) == 1)
776+
$uniqueRules[2] = $field;
777+
else
778+
$uniqueRules[2] = $params[1];
779+
780+
if (isset($this->primaryKey)) {
781+
$uniqueRules[3] = $this->{$this->primaryKey};
782+
$uniqueRules[4] = $this->primaryKey;
775783
}
776-
}
784+
else {
785+
$uniqueRules[3] = $this->id;
786+
}
787+
788+
$rule = 'unique:' . implode(',', $uniqueRules);
789+
} // end if strpos unique
790+
791+
} // end foreach ruleset
777792
}
778-
793+
779794
return $rules;
780795
}
781796

0 commit comments

Comments
 (0)