diff --git a/src/Ardent/Ardent.php b/src/Ardent/Ardent.php index 4659181..e15e508 100755 --- a/src/Ardent/Ardent.php +++ b/src/Ardent/Ardent.php @@ -821,39 +821,43 @@ protected function buildUniqueExclusionRules(array $rules = array()) { $ruleset = (is_string($ruleset))? explode('|', $ruleset) : $ruleset; foreach ($ruleset as &$rule) { - if (strpos($rule, 'unique:') === 0) { - // Stop splitting at 4 so final param will hold optional where clause - $params = explode(',', $rule, 4); - - $uniqueRules = array(); - - // Append table name if needed - $table = explode(':', $params[0]); - if (count($table) == 1) - $uniqueRules[1] = $this->getTable(); - 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}; - - // If optional where rules are passed, append them otherwise use primary key - $uniqueRules[4] = isset($params[3]) ? $params[3] : $this->primaryKey; - } - else { - $uniqueRules[3] = $this->id; + if (strpos($rule, 'unique:') === 0) { + // Stop splitting at 4 so final param will hold optional where clause + $params = explode(',', $rule, 4); + + $uniqueRules = array(); + + // Append table name if needed + $table = explode(':', $params[0]); + if (count($table) == 1) { + $uniqueRules[1] = $this->getTable(); + } 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)) { + if (isset($this->{$this->primaryKey})) { + $uniqueRules[3] = $this->{$this->primaryKey}; + + // If optional where rules are passed, append them otherwise use primary key + $uniqueRules[4] = isset($params[3])? $params[3] : $this->primaryKey; + } + } else { + if (isset($this->id)) { + $uniqueRules[3] = $this->id; + } + } + + $rule = 'unique:'.implode(',', $uniqueRules); } - - $rule = 'unique:' . implode(',', $uniqueRules); - } // end if strpos unique - - } // end foreach ruleset + } } return $rules;