/** * Return the current element * @return EMongoDocument * @since v1.3.4 */ public function current() { $document = $this->_cursor->current(); if (empty($document)) { return $document; } return $this->_model->populateRecord($document); }
public static function getCursorAsArray(EMongoDocument $model, $fn) { $useCursor = $model->getUseCursor(); $model->setUseCursor(TRUE); $result = $fn($model)->getCursor(); $model->setUseCursor($useCursor); return $result; }
/** * Gets the active record for the current row * @return EMongoDocument|mixed * @throws EMongoException */ public function current() { if (!$this->run) { if ($this->model->getDbConnection()->queryCachingCount > 0 && $this->model->getDbConnection()->queryCachingDuration > 0 && $this->model->getDbConnection()->queryCacheID !== false && ($cache = Yii::app()->getComponent($this->model->getDbConnection()->queryCacheID)) !== null) { $this->model->getDbConnection()->queryCachingCount--; $info = $this->cursor()->info(); $cacheKey = 'yii:dbquery' . $this->model->getDbConnection()->server . ':' . $this->model->getDbConnection()->db . ':' . $this->model->getDbConnection()->getSerialisedQuery(is_array($info['query']) && isset($info['query']['$query']) ? $info['query']['$query'] : array(), $info['fields'], is_array($info['query']) && isset($info['query']['$orderby']) ? $info['query']['$orderby'] : array(), $info['skip'], $info['limit']) . ':' . $this->model->getCollection(); if (($result = $cache->get($cacheKey)) !== false) { Yii::trace('Query result found in cache', 'extensions.MongoYii.EMongoDocument'); $this->cachedArray = $result; $this->fromCache = true; } else { $this->cachedArray = iterator_to_array($this->cursor); } } if (isset($cache, $cacheKey)) { $cache->set($cacheKey, $this->cachedArray, $this->model->getDbConnection()->queryCachingDuration, $this->model->getDbConnection()->queryCachingDependency); $this->fromCache = true; } $this->run = true; } if ($this->model === null) { throw new EMongoException(Yii::t('yii', 'The MongoCursor must have a model')); } if ($this->fromCache) { return $this->current = $this->model->populateRecord(current($this->cachedArray), true, $this->partial); } return $this->current = $this->model->populateRecord($this->cursor()->current(), true, $this->partial); }
/** * Gets the active record for the current row * @return EMongoDocument|mixed * @throws EMongoException */ public function current() { if ($this->model === null) { throw new EMongoException(Yii::t('yii', 'The MongoCursor must have a model')); } return $this->current = $this->model->populateRecord($this->cursor()->current(), true, $this->partial); }
/** * 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'; } }
/** * Gets the active record for the current row * @return EMongoDocument|mixed * @throws EMongoException */ public function current() { if ($this->model === null) { return $this->current = $this->cursor->current(); } else { return $this->current = $this->model->populateRecord($this->cursor->current(), true, $this->partial); } }
/** * Returns whether there is an element at the specified offset. * This method is required by the interface ArrayAccess. * @param mixed $offset the offset to check on * @return boolean */ public function offsetExists($offset) { if (!isset($this->_array)) { foreach ($this->_cursor as $document) { $this->offsetSet(null, $this->_model->populateRecord($document)); } } if (isset($this->_array[$offset])) { return true; } else { return false; } }
/** * 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; }
protected function beforeSave() { if (parent::beforeSave()) { if ($this->isNewRecord) { $this->id = $this->getAutoIncreaseId(false); } if (isset($this['last_uid'])) { $this->last_uid = Yii::app()->user->id; //添加操作者 } if (isset($this['update_time'])) { $this->update_time = time(); } return true; } else { return false; } }
/** * 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 { } }
/** * Метод для приведения типов перед сохранением в MongoDB */ protected function beforeSave() { if (parent::beforeSave()) { $arrayType = $this->returnArrayType(); foreach ($arrayType as $name => $type) { switch ($type) { case 'integer': $this->{$name} = intval($this->{$name}); break; case 'float': $this->{$name} = floatval($this->{$name}); break; case 'boolean': $this->{$name} = (bool) $this->{$name}; break; } } return true; } else { return false; } }
/** * 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)); } }
protected function beforeSave() { if (parent::beforeSave()) { if (empty($this->itemid)) { $this->itemid = md5(uniqid()); } if (empty($this->url)) { $this->url = '#'; } if (!isset($this->active)) { $this->active = false; } if (!isset($this->visible)) { $this->visible = true; } if (!isset($this->descriptionashint)) { $this->descriptionashint = true; } if (!isset($this->labels)) { $this->labels = $this->asa('attributesBehavior')->initLanguageAttribute('labels'); } if (!isset($this->descriptions)) { $this->descriptions = $this->asa('attributesBehavior')->initLanguageAttribute('descriptions'); } return true; } else { return false; } }
public function beforeSave() { if (Yii::app()->controller->action->id == "subscribe") { if (in_array("neuropathologiste", $this->profil) || in_array("geneticien", $this->profil) || in_array("chercheur", $this->profil)) { $this->profil = []; } } return parent::beforeSave(); }
/** * Return the actual list of soft attributes being used by this model * @return array list of initialized soft attributes * @since v1.3.4 */ public function getSoftAttributeNames() { return array_diff(array_keys($this->softAttributes), parent::attributeNames()); }
/** * 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; } }
protected function instantiate($attributes) { $model = parent::instantiate($attributes); $loadedFields = array_keys($attributes); if (count($loadedFields) < count($model->attributeNames())) { $model->_partial = true; $model->_loadedFields = $loadedFields; } return $model; }
/** * Construct with parameter. * * @param \CActiveRecord|\EMongoDocument $model * @param bool $asIs if set this flag then relations array will return without filtering * @return array of relations parameters. */ public function getWithParameters($model, $asIs = false) { if (isset($_GET[self::WITH])) { $temp = explode(",", \Yii::app()->request->getParam(self::WITH)); if ($asIs) { return $temp; } foreach ($temp as $key => $value) { if (!isset($model->relations()[$value])) { unset($temp[$key]); } } $temp = Filter::filterWithParameters($temp, get_class($model)); return $temp; } else { return array(); } }
/** * Set EMongoDB component instance * @param EMongoDB $component * @since v1.0 */ public function setMongoDBComponent(EMongoDB $component) { self::$_emongoDb = $component; }
public function afterValidate() { parent::afterValidate(); $this->onAfterValidate(new CEvent($this)); }
/** * Counts the records returned by the criteria. By default this will not take skip and limit into account * you can add inject true as the first and only parameter to enable MongoDB to take those offsets into * consideration. * * @param bool $takeSkip * @return int */ public function count($takeSkip = false) { return $this->model->getCollection()->count($this->query, $this->options); }
public function guessNameColumn(EMongoDocument $model) { foreach ($model->attributeNames() as $name) { if (!strcasecmp($name, 'name')) { return $name; } } foreach ($model->attributeNames() as $name) { if (!strcasecmp($name, 'title')) { return $name; } } foreach ($model->attributeNames() as $name) { if ($name == $model->primaryKey()) { return $name; } } return '_id'; }
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; }
/** * Creates an EMongoGridFS with the given attributes. * This method is internally used by the find methods. * @param MongoGridFSFile $document mongo gridFSFile * @param array $attributes attribute values (column name=>column value) * @param boolean $callAfterFind whether to call {@link afterFind} after the record is populated. * This parameter is added in version 1.0.3. * @return EMongoDocument the newly created document. The class of the object is the same as the model class. * Null is returned if the input data is false. * @since v1.3 */ public function populateRecord($document, $callAfterFind = true) { Yii::trace('Trace: ' . __CLASS__ . '::' . __FUNCTION__ . '()', 'ext.MongoDb.EMongoGridFS'); if ($document instanceof MongoGridFSFile) { $model = parent::populateRecord($document->file, $callAfterFind); $model->_gridFSFile = $document; return $model; } else { return parent::populateRecord($document, $callAfterFind); } }
/** * Magic will either call a function on the file if it exists or bubble to parent * @see EMongoDocument::__call() */ public function __call($name, $parameters) { if ($this->getFile() instanceof MongoGridFSFile && method_exists($this->getFile(), $name)) { return call_user_func_array(array($this->getFile(), $name), $parameters); } return parent::__call($name, $parameters); }
public static function model($className = __CLASS__) { return parent::model($className); }
/** * Compares current active record with another one. * The comparison is made by comparing table name and the primary key values of the two active records. * @param EMongoDocument $record - record to compare to * @return boolean - whether the two active records refer to the same row in the database table. */ public function equals($record) { return $this->collectionName() === $record->collectionName() && (string) $this->getPrimaryKey() === (string) $record->getPrimaryKey(); }