Exemplo n.º 1
0
 /**
  * Validates the attribute of the object.
  * If there is any error, the error message is added to the object.
  * @param CModel $object the object being validated
  * @param string $attribute the attribute being validated
  */
 protected function validateAttribute($object, $attribute)
 {
     $value = $object->{$attribute};
     if ($this->allowEmpty && $this->isEmpty($value)) {
         return;
     }
     $className = $this->className === null ? get_class($object) : Yii::import($this->className);
     $attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
     $finder = Document::model($className);
     $criteria = array($attributeName => $this->mongoId ? new ObjectID($value) : $value);
     if (!$finder->exists($criteria)) {
         $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" is invalid.');
         $this->addError($object, $attribute, $message, array('{value}' => CHtml::encode($value)));
     }
 }
Exemplo n.º 2
0
 /**
  * @see CSort::resolveLabel()
  * @param string $attribute
  * @return string
  */
 public function resolveLabel($attribute)
 {
     $definition = $this->resolveAttribute($attribute);
     if (is_array($definition)) {
         if (isset($definition['label'])) {
             return $definition['label'];
         }
     } elseif (is_string($definition)) {
         $attribute = $definition;
     }
     if ($this->modelClass !== null) {
         return Document::model($this->modelClass)->getAttributeLabel($attribute);
     }
     return $attribute;
 }
Exemplo n.º 3
0
 /**
  * The cursor constructor
  * @param string|EMongoDocument $modelClass - The class name for the active record
  * @param array|MongoCursor|EMongoCriteria $criteria -  Either a condition array (without sort,limit and skip) or a MongoCursor Object
  * @param array $fields
  */
 public function __construct($modelClass, $cursor, $options = [])
 {
     if (is_string($modelClass)) {
         $this->modelClass = $modelClass;
         $this->model = Document::model($this->modelClass);
     } elseif ($modelClass instanceof Document) {
         $this->modelClass = get_class($modelClass);
         $this->model = $modelClass;
     }
     $it = new IteratorIterator($cursor);
     $it->rewind();
     $this->cursor = $it;
     if ($options['partial']) {
         $this->partial = true;
     }
 }
Exemplo n.º 4
0
 /**
  * Creates the EMongoDataProvider instance
  * @param string|Document $modelClass
  * @param array $config
  */
 public function __construct($modelClass, $config = array())
 {
     if (is_string($modelClass)) {
         $this->modelClass = $modelClass;
         $this->model = Document::model($this->modelClass);
     } elseif ($modelClass instanceof Document) {
         $this->modelClass = get_class($modelClass);
         $this->model = $modelClass;
     }
     $this->setId($this->modelClass);
     foreach ($config as $key => $value) {
         $this->{$key} = $value;
     }
     if (!$this->getCriteria()) {
         $this->setCriteria(new Query());
     }
 }
Exemplo n.º 5
0
 /**
  * Validates the attribute of the object.
  * If there is any error, the error message is added to the object.
  * @param CModel $object the object being validated
  * @param string $attribute the attribute being validated
  */
 protected function validateAttribute($object, $attribute)
 {
     $value = $object->{$attribute};
     if ($this->allowEmpty && $this->isEmpty($value)) {
         return;
     }
     $className = $this->className === null ? get_class($object) : Yii::import($this->className);
     $attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
     // We get a RAW document here to prevent the need to make yet another active record instance
     $doc = Document::model($className)->getCollection()->findOne(array_merge($this->criteria, array($attributeName => $this->caseSensitive ? $value : new Regex($value, 'i'))));
     // If a doc was found first test if the unique attribute is the primaryKey
     // If we are uniquely id'ing the pk then check this is a new record and not
     // an old one and check to see if the pks are the same
     // If the found doc is not evaledd onm pk then make sure the two pks are not the same
     if ($doc && ($attributeName === $object->primaryKey() && $object->getIsNewRecord() && (string) $doc[$object->primaryKey()] == (string) $object->getPrimaryKey() || (string) $doc[$object->primaryKey()] != (string) $object->getPrimaryKey())) {
         // Then it ain't unique
         $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" has already been taken.');
         $this->addError($object, $attribute, $message, array('{value}' => CHtml::encode($value)));
     } else {
     }
 }
Exemplo n.º 6
0
 /**
  * Returns the static model of the specified AR class.
  * 
  * @return User the static model class
  */
 public static function model($className = __CLASS__)
 {
     return parent::model($className);
 }
Exemplo n.º 7
0
 /**
  * Returns the text label for the specified attribute.
  * This method overrides the parent implementation by supporting
  * returning the label defined in relational object.
  * In particular, if the attribute name is in the form of "post.author.name",
  * then this method will derive the label from the "author" relation's "name" attribute.
  * @see CModel::generateAttributeLabel()
  * @param string $attribute - the attribute name
  * @return string - the attribute label
  */
 public function getAttributeLabel($attribute)
 {
     $labels = $this->attributeLabels();
     if (isset($labels[$attribute])) {
         return $labels[$attribute];
     }
     if (strpos($attribute, '.') === false) {
         return $this->generateAttributeLabel($attribute);
     }
     $segs = explode('.', $attribute);
     $name = array_pop($segs);
     $model = $this;
     foreach ($segs as $seg) {
         $relations = $model->relations();
         if (!isset($relations[$seg])) {
             break;
         }
         $model = Document::model($relations[$seg][1]);
     }
     return $model->getAttributeLabel($name);
 }