Procházet zdrojové kódy

Adding count function

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

+ 24 - 0
lib/Mooses/AbstractMongo.php

@@ -263,6 +263,30 @@ class Mooses_AbstractMongo extends Mooses_Mongodb_Mongo_Document {
         return new Mooses_Mongodb_Mongo_Iterator_Cursor($cursor, $config);
     }
 
+    /**
+     * @param $_pageLimit
+     * @param $_pageSize
+     * @return int
+     * @throws Mooses_Mongodb_Mongo_Exception
+     */
+    protected static function countResults($query){
+        $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;
+        }
+
+        return static::getMongoCollection(false)->count($query);
+    }
+
+    protected function ___countCollection(){
+        return self::countResults($this->_queryConditions);
+    }
+
     protected function ___getCollectionData($_forceArray = true){
         if($this->_pageLimit !== false && $this->_page !== false) {
             $_result = self::fetchAllWithLimits($this->_queryConditions, $this->_fields, $this->_pageLimit, $this->_page);