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

Commit

Permalink
Fixing unique rules when there's no ID to query
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsantos07 committed Nov 30, 2015
1 parent 678dd2e commit 73fba06
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/Ardent/Ardent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 73fba06

Please sign in to comment.