From ffdf8ecf51908382c4f45296e5375e3c7f01020c Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 21 Oct 2013 12:01:28 -0700 Subject: [PATCH] Addresses #86 miss --- src/LaravelBook/Ardent/Ardent.php | 45 ++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index dbec390..9236453 100644 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -759,23 +759,38 @@ protected function buildUniqueExclusionRules(array $rules = array()) { $ruleset = (is_string($ruleset))? explode('|', $ruleset) : $ruleset; foreach ($ruleset as &$rule) { - if (strpos($rule, 'unique') === 0) { - $params = explode(',', $rule); - - // Append field name if needed - if (count($params) == 1) { - $params[1] = $field; - } - - // if the 3rd param was set, do not overwrite it - if (!is_numeric(@$params[2])) $params[2] = $this->id; - - - $rule = implode(',', $params); + if (strpos($rule, 'unique') === 0) { + $params = explode(',', $rule); + + $uniqueRules = array(); + + // Append table name if needed + $table = explode(':', $params[0]); + if (count($table) == 1) + $uniqueRules[1] = $this->table; + else + $uniqueRules[1] = $table[1]; + + // Append field name if needed + if (count($params) == 1) + $uniqueRules[2] = $field; + else + $uniqueRules[2] = $params[1]; + + if (isset($this->primaryKey)) { + $uniqueRules[3] = $this->{$this->primaryKey}; + $uniqueRules[4] = $this->primaryKey; } - } + else { + $uniqueRules[3] = $this->id; + } + + $rule = 'unique:' . implode(',', $uniqueRules); + } // end if strpos unique + + } // end foreach ruleset } - + return $rules; }