Procházet zdrojové kódy

Adding pagination methods

Paolo Libertini před 6 roky
rodič
revize
b1e02246ad
1 změnil soubory, kde provedl 47 přidání a 1 odebrání
  1. 47 1
      lib/Mooses/AbstractMongo.php

+ 47 - 1
lib/Mooses/AbstractMongo.php

@@ -30,6 +30,8 @@ class Mooses_AbstractMongo extends Mooses_Mongodb_Mongo_Document {
     public $loadKey = array();
     protected $_dataUpdate = array();
     protected $_sendAsArray = false;
+    protected $_pageLimit = false;
+    protected $_page = false;
 
     public function __construct($_data = []) {
         $this->_setCollectionAndDatabase();
@@ -150,6 +152,16 @@ class Mooses_AbstractMongo extends Mooses_Mongodb_Mongo_Document {
         }
     }
 
+    protected function ___setPageSize($_limit){
+        $this->_pageLimit = $_limit;
+        return $this;
+    }
+
+    protected function ___setPage($_page){
+        $this->_page = $_page;
+        return $this;
+    }
+
     public static function __callStatic($name, $arguments)
     {
         if(get_called_class() != get_class(self::$_instance)){
@@ -221,8 +233,42 @@ class Mooses_AbstractMongo extends Mooses_Mongodb_Mongo_Document {
         return $this;
     }
 
+    /**
+     * @param $_pageLimit
+     * @param $_pageSize
+     * @return Mooses_Mongodb_Mongo_Iterator_Cursor
+     * @throws MongoCursorException
+     * @throws Mooses_Mongodb_Mongo_Exception
+     */
+    protected static function fetchAllWithLimits($_pageLimit, $_pageSize){
+        $inheritance = static::getCollectionInheritance();
+        if (count($inheritance) > 1) {
+            $query['_type'] = $inheritance[0];
+        }
+
+        // If we are selecting specific fields make sure _type is always there
+        if (!empty($fields) && !isset($fields['_type'])) {
+            $fields['_type'] = 1;
+        }
+
+        $cursor = static::getMongoCollection(false)->find($query, $fields)->limit($_pageLimit)->skip($_pageSize);
+
+        $config = array();
+        $config['connectionGroup'] = static::getConnectionGroupName();
+        $config['db'] = static::getDbName();
+        $config['collection'] = static::getCollectionName();
+        $config['documentClass'] = static::getDocumentClass();
+        $config['documentSetClass'] = static::getDocumentSetClass();
+
+        return new Mooses_Mongodb_Mongo_Iterator_Cursor($cursor, $config);
+    }
+
     protected function ___getCollectionData($_forceArray = true){
-        $_result = $this->fetchAll($this->_queryConditions, $this->_fields);
+        if($this->_pageLimit !== false && $this->_page !== false) {
+            $_result = self::fetchAllWithLimits($this->_pageLimit, $this->_page);
+        } else {
+            $_result = $this->fetchAll($this->_queryConditions, $this->_fields);
+        }
         if (count($this->_sorterArray) == 0) {
             return $this->_convertMongoCursor($_result, $_forceArray);
         } else {