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

Commit 73fba06

Browse files
committed
Fixing unique rules when there's no ID to query
1 parent 678dd2e commit 73fba06

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/Ardent/Ardent.php

+36-32
Original file line numberDiff line numberDiff line change
@@ -821,39 +821,43 @@ protected function buildUniqueExclusionRules(array $rules = array()) {
821821
$ruleset = (is_string($ruleset))? explode('|', $ruleset) : $ruleset;
822822

823823
foreach ($ruleset as &$rule) {
824-
if (strpos($rule, 'unique:') === 0) {
825-
// Stop splitting at 4 so final param will hold optional where clause
826-
$params = explode(',', $rule, 4);
827-
828-
$uniqueRules = array();
829-
830-
// Append table name if needed
831-
$table = explode(':', $params[0]);
832-
if (count($table) == 1)
833-
$uniqueRules[1] = $this->getTable();
834-
else
835-
$uniqueRules[1] = $table[1];
836-
837-
// Append field name if needed
838-
if (count($params) == 1)
839-
$uniqueRules[2] = $field;
840-
else
841-
$uniqueRules[2] = $params[1];
842-
843-
if (isset($this->primaryKey)) {
844-
$uniqueRules[3] = $this->{$this->primaryKey};
845-
846-
// If optional where rules are passed, append them otherwise use primary key
847-
$uniqueRules[4] = isset($params[3]) ? $params[3] : $this->primaryKey;
848-
}
849-
else {
850-
$uniqueRules[3] = $this->id;
824+
if (strpos($rule, 'unique:') === 0) {
825+
// Stop splitting at 4 so final param will hold optional where clause
826+
$params = explode(',', $rule, 4);
827+
828+
$uniqueRules = array();
829+
830+
// Append table name if needed
831+
$table = explode(':', $params[0]);
832+
if (count($table) == 1) {
833+
$uniqueRules[1] = $this->getTable();
834+
} else {
835+
$uniqueRules[1] = $table[1];
836+
}
837+
838+
// Append field name if needed
839+
if (count($params) == 1) {
840+
$uniqueRules[2] = $field;
841+
} else {
842+
$uniqueRules[2] = $params[1];
843+
}
844+
845+
if (isset($this->primaryKey)) {
846+
if (isset($this->{$this->primaryKey})) {
847+
$uniqueRules[3] = $this->{$this->primaryKey};
848+
849+
// If optional where rules are passed, append them otherwise use primary key
850+
$uniqueRules[4] = isset($params[3])? $params[3] : $this->primaryKey;
851+
}
852+
} else {
853+
if (isset($this->id)) {
854+
$uniqueRules[3] = $this->id;
855+
}
856+
}
857+
858+
$rule = 'unique:'.implode(',', $uniqueRules);
851859
}
852-
853-
$rule = 'unique:' . implode(',', $uniqueRules);
854-
} // end if strpos unique
855-
856-
} // end foreach ruleset
860+
}
857861
}
858862

859863
return $rules;

0 commit comments

Comments
 (0)