Skip to content

Commit

Permalink
Merge pull request #341 from piotr-cz/fix-Unique_fields
Browse files Browse the repository at this point in the history
Unique fields bugfix
  • Loading branch information
aheinze committed Jul 17, 2015
2 parents 6b178d7 + 8d42136 commit 7fc70e5
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions modules/core/Collections/Controller/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,28 @@ public function saveentry(){
$col = "collection".$collection["_id"];

// Check for uniqueeness of fields set as unique
foreach ($collection['fields'] as $fieldDefinition) {

$fieldName = $fieldDefinition['name'];

if (
$fieldDefinition['unique'] &&
isset($entry[$fieldName]) &&
$this->app->db->findOne("collections/{$col}", [$fieldName => $entry[$fieldName]])
) {
$this->app->response->status = 409;

return sprintf(
$this->app->helper('i18n')->get("There is already an entry in this collection with this slug for field '%s'."),
$fieldName
);
foreach ($collection["fields"] as $fieldDefinition) {

$slugName = $fieldDefinition["name"] . "_slug";

if (!empty($fieldDefinition["slug"]) && !empty($fieldDefinition["unique"]) && isset($entry[$slugName])) {

$collision = $this->app->db->findOne("collections/{$col}", [$slugName => $entry[$slugName]]);

if (!$collision) {
continue;
}

// New and colliding or other entry
if (!isset($entry["_id"]) || $entry["_id"] != $collision["_id"]) {

$this->app->response->status = 409;

return sprintf(
$this->app->helper("i18n")->get("There is already an entry in this collection with this slug for field '%s'."),
$fieldDefinition["label"]
);
}
}
}

Expand Down

0 comments on commit 7fc70e5

Please sign in to comment.