Skip to content
Permalink
Browse files

Added option to check for NULL values on IsUnique

  • Loading branch information...
JayPHP JayPHP
JayPHP authored and JayPHP committed Apr 8, 2016
1 parent d5d9a03 commit 9989334d7bcb5d8d055d3e862ead7c79ce2b2876
Showing with 13 additions and 1 deletion.
  1. +11 −0 src/ORM/Rule/IsUnique.php
  2. +2 −1 src/ORM/RulesChecker.php
@@ -53,6 +53,7 @@ public function __invoke(EntityInterface $entity, array $options)
return true;
}
$checkNull = isset($options['checkNull']) ? true : false;
$alias = $options['repository']->alias();
$conditions = $this->_alias($alias, $entity->extract($this->_fields));
if ($entity->isNew() === false) {
@@ -63,6 +64,16 @@ public function __invoke(EntityInterface $entity, array $options)
}
}
if($checkNull) {
// Thanks to ndm
foreach ($conditions as $key => $value) {
if ($value === null) {
$conditions[$key . ' IS'] = $value;
unset($conditions[$key]);
}
}
}
return !$options['repository']->exists($conditions);
}
@@ -52,7 +52,8 @@ public function isUnique(array $fields, $message = null)
}
}
$errorField = current($fields);
$errorField = isset($fields['errorField']) ? $fields['errorField'] : false;
return $this->_addError(new IsUnique($fields), '_isUnique', compact('errorField', 'message'));
}

0 comments on commit 9989334

Please sign in to comment.
You can’t perform that action at this time.