Exemplo n.º 1
0
 /**
  * MongoQuery::instance()
  *
  * @param mixed $collectionName
  * @return
  */
 public static function instance($collectionName)
 {
     if (!isset(self::$_instance)) {
         self::$_instance = array();
     }
     if (!isset(self::$_instance[$collectionName])) {
         self::$_instance[$collectionName] = new MongoQuery($collectionName);
     }
     return self::$_instance[$collectionName];
 }
 /**
  * Fetches the data from the collection
  * Uses the assigned cursor
  *
  * @return array list of data items
  */
 protected function fetchData()
 {
     $data = array();
     if (empty($this->_cursor)) {
         return $data;
     }
     if (($sort = $this->getSort()) !== false && ($order = $sort->getOrderBy()) != '') {
         $sort = array();
         foreach ($this->getSortDirections($order) as $name => $descending) {
             $sort[$name] = $descending ? -1 : 1;
         }
         $this->_cursor->sort($sort);
     }
     if (($pagination = $this->getPagination()) !== false) {
         $pagination->setItemCount($this->getTotalItemCount());
         $limit = $pagination->pageSize;
         $skip = $pagination->currentPage * $limit;
         $this->_cursor->limit($limit)->skip($skip);
     }
     $modelProperties = null;
     //get the array of public, non static propertyNames of the _objectClassName instance
     if (isset($this->_objectClassName) && $this->_objectClassName != 'stdClass') {
         $modelInstance = new $this->_objectClassName();
         if (!$modelInstance instanceof CModel) {
             //CModel uses setAttributes in MongoQuery::arrayToModel
             $reflectionClass = new ReflectionClass($modelInstance);
             foreach ($reflectionClass->getProperties() as $property) {
                 if ($property->isPublic() && !$property->isStatic()) {
                     $modelProperties[] = $property->getName();
                 }
             }
         }
     }
     // fetch data
     foreach ($this->_cursor as $id => $value) {
         if (isset($this->_objectClassName)) {
             //convert to a object
             if ($this->_objectClassName == 'stdClass') {
                 $data[] = (object) $value;
                 //add as stdClass
             } else {
                 $data[] = MongoQuery::arrayToModel($value, $this->_objectClassName, $modelProperties);
             }
         } else {
             $data[] = $value;
         }
     }
     return $data;
 }