The model returned is a static instance of the AR class.
It is provided for invoking class-level methods (something similar to static class methods.)
EVERY derived AR class must override this method as follows,
public static function model($className=__CLASS__) { return parent::model($className); }
/** * Constructor. * @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance * (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>). * @param array $config configuration (name=>value) to be applied as the initial property values of this class. * @since v1.0 */ public function __construct($modelClass, $config = array()) { if (is_string($modelClass)) { $this->modelClass = $modelClass; $this->model = EMongoDocument::model($modelClass); } else { if ($modelClass instanceof EMongoDocument) { $this->modelClass = get_class($modelClass); $this->model = $modelClass; } else { throw new EMongoException('Invalid model type for ' . __CLASS__); } } $this->_criteria = $this->model->getDbCriteria(); if (isset($config['criteria'])) { $this->_criteria->mergeWith($config['criteria']); unset($config['criteria']); } $this->setId($this->modelClass); foreach ($config as $key => $value) { $this->{$key} = $value; } if ($this->keyField !== null && is_array($this->keyField)) { throw new EMongoException('This DataProvider cannot handle multi-field primary key.'); } else { $this->keyField = '_id'; } }
/** * 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, $criteria = array(), $fields = array()) { // If $fields has something in it if (!empty($fields)) { $this->partial = true; } if (is_string($modelClass)) { $this->modelClass = $modelClass; $this->model = EMongoDocument::model($this->modelClass); } elseif ($modelClass instanceof EMongoDocument) { $this->modelClass = get_class($modelClass); $this->model = $modelClass; } if ($criteria instanceof MongoCursor) { $this->cursor = $criteria; $this->cursor->reset(); } elseif ($criteria instanceof EMongoCriteria) { $this->criteria = $criteria; $this->cursor = $this->model->getCollection()->find($criteria->condition, $criteria->project)->sort($criteria->sort); if ($criteria->skip > 0) { $this->cursor->skip($criteria->skip); } if ($criteria->limit > 0) { $this->cursor->limit($criteria->limit); } } else { // Then we are doing an active query $this->criteria = $criteria; $this->cursor = $this->model->getCollection()->find($criteria, $fields); } }
/** * 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($model, $criteria = [], $fields = []) { if (is_string($model)) { $this->model = EMongoDocument::model($this->modelClass); } elseif ($model instanceof EMongoDocument) { $this->model = $model; } $this->options = []; if ($criteria instanceof EMongoCriteria) { $this->options['projection'] = $criteria->project; if ($criteria->skip > 0) { $this->options['skip'] = $criteria->skip; } if ($criteria->limit > 0) { $this->options['limit'] = intval($criteria->limit); } if ($criteria->sort) { $this->options['sort'] = $criteria->sort; } $this->query = $criteria->condition; } else { // Then we are doing an active query $this->options['projection'] = $fields; $this->query = $criteria; } }
/** * Creates the EMongoDataProvider instance * @param string|EMongoDocument $modelClass * @param array $config */ public function __construct($modelClass, $config = array()) { if (is_string($modelClass)) { $this->modelClass = $modelClass; $this->model = EMongoDocument::model($this->modelClass); } elseif ($modelClass instanceof EMongoDocument) { $this->modelClass = get_class($modelClass); $this->model = $modelClass; } $this->setId($this->modelClass); foreach ($config as $key => $value) { $this->{$key} = $value; } }
/** * 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 = EMongoDocument::model($className); $criteria = array($attributeName => $this->mongoId ? new MongoId($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))); } }
/** * @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 EMongoDocument::model($this->modelClass)->getAttributeLabel($attribute); } return $attribute; }
/** * 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 = EMongoDocument::model($className)->getCollection()->findOne(array_merge($this->criteria, [$attributeName => $this->caseSensitive ? $value : ['$regex' => $value, '$options' => 'i']])); // If a doc was fund and it isn't this doc, as decided by the primnary key if ($doc && (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, ['{value}' => CHtml::encode($value)]); } else { } }
/** * 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 = EMongoDocument::model($className)->getCollection()->findOne(array_merge($this->criteria, array($attributeName => $this->caseSensitive ? $value : new MongoRegex('/' . $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 { } }
/** * 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 = EMongoDocument::model($className); $criteria = new EMongoCriteria(); $criteria->{$attribute} = $value; if ($this->criteria !== array()) { $criteria->mergeWith($this->criteria); } if (!$object instanceof EMongoDocument || $object->isNewRecord) { $exists = $finder->exists($criteria); } else { $criteria->limit = 2; $objects = $finder->findAll($criteria); $n = count($objects); if ($n === 1) { if ($column->isPrimaryKey) { // primary key is modified and not unique $exists = $object->getOldPrimaryKey() != $object->getPrimaryKey(); } else { // non-primary key, need to exclude the current record based on PK $exists = $objects[0]->getPrimaryKey() != $object->getOldPrimaryKey(); } } else { $exists = $n > 1; } } if ($exists) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" has already been taken.'); $this->addError($object, $attribute, $message, array('{value}' => $value)); } }
public static function model($className = __CLASS__) { return parent::model($className); }
public function actionGetRequestInfo($className, $keyName, $key) { $requestModel = EMongoDocument::model($className)->findByAttributes(array($keyName => $key)); $retArr = array(); $widget = new CTextHighlighter(); $widget->language = 'xml'; $retArr['methodName'] = $requestModel->methodName; $retArr['requestXml'] = $widget->highlight($requestModel->requestXml); $retArr['responseXml'] = $widget->highlight($requestModel->responseXml); //$retArr['requestUrl'] = $requestModel->requestUrl; $retArr['timestamp'] = date("Y-m-d H:i:s", $requestModel->timestamp); $retArr['executionTime'] = Yii::app()->format->formatNumber($requestModel->executionTime); $retArr['errorDescription'] = $requestModel->errorDescription; echo json_encode($retArr); die; }
/** * Returns the specified EMongoDocument instance in the fixture data. * @param string $name the fixture name * @param string $alias the alias for the fixture data document * @return EMongoDocument the MongoDocument instance. False is returned * if there is no such fixture document. */ public function getRecord($name, $alias) { if (isset($this->_records[$name][$alias])) { if (is_string($this->_records[$name][$alias])) { $row = $this->_rows[$name][$alias]; $model = EMongoDocument::model($this->_records[$name][$alias]); $pk = $row['_id']; $this->_records[$name][$alias] = $model->findByPk($pk); } return $this->_records[$name][$alias]; } else { return false; } }
/** * @param string $className * @return static */ public static function model($className = '') { return parent::model($className ?: get_called_class()); }
/** * 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 = EMongoDocument::model($relations[$seg][1]); } return $model->getAttributeLabel($name); }