Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added unique validator for mongo databases

  • Loading branch information...
commit 3f8d8f0b975a277eb8b53cc28970754cdcf4b23a 1 parent 2f23384
@canni canni authored
Showing with 24 additions and 0 deletions.
  1. +24 −0 extra/EMongoUniqueValidator.php
View
24 extra/EMongoUniqueValidator.php
@@ -0,0 +1,24 @@
+<?php
+
+class EMongoUniqueValidator extends CValidator
+{
+ public $allowEmpty=true;
+
+ public function validateAttribute($object, $attribute)
+ {
+ $value = $object->{$attribute};
+ if($this->allowEmpty && ($value === null || $value === ''))
+ return;
+
+ $criteria = new EMongoCriteria;
+ $criteria->{$attribute} = $value;
+ $count = $object::model()->count($criteria);
+
+ if($count !== 0)
+ $this->addError(
+ $object,
+ $attribute,
+ Yii::t('yii', '{attribute} is not unique in DB.')
+ );
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.