Example #1
0
 public static function getStatusesArr($type = 'application')
 {
     $arr = array();
     $obj = new self();
     /* @var $collection Monogo_Application_Model_Resource_Application_Status_Collection */
     $collection = $obj->getCollection();
     $collection->addFieldToFilter('type', $type);
     $collection->getSelect()->order("main_table.updated DESC");
     $collection->load();
     foreach ($collection as $row) {
         $arr[$row->getEntityId()] = $row->getStatusName();
     }
     return $arr;
 }
Example #2
0
 public static function group($collection, $keys = [], $query = [], $count = [], $sum = [], $sort = [], $limit = null)
 {
     global $debug;
     // Turn keys into an array if is isn't already an array
     if (!is_array($keys)) {
         $keys = [$keys];
     }
     // Start the aggregation pipeline with the query
     $pipeline = [];
     if (sizeof($query)) {
         $pipeline[] = ['$match' => $query];
     }
     // Create the group by using the given key(s)
     $ids = [];
     foreach ($keys as $key => $value) {
         if (is_numeric($key)) {
             $ids[] = '$' . $value;
         } else {
             $ids[$key] = ['$' . $key => '$' . $value];
         }
     }
     if (sizeof($ids) == 1 && isset($ids[0])) {
         $ids = $ids[0];
     }
     $group = [];
     $group['_id'] = $ids;
     // If no counts or sums are given, assume a count based on the keys for the $group
     if (sizeof($count) == 0 && sizeof($sum) == 0) {
         $group['count'] = ['$sum' => 1];
     }
     // Include counts in the $group
     if (!is_array($count)) {
         $count = [$count];
     }
     foreach ($count as $s) {
         $group[str_replace('.', '_', $s) . 'Count'] = ['$sum' => 1];
     }
     // Include sums in the $group
     if (!is_array($sum)) {
         $sum = [$sum];
     }
     foreach ($sum as $s) {
         $group[str_replace('.', '_', $s) . 'Sum'] = ['$sum' => '$' . $s];
     }
     // Add the group to the pipeline
     $pipeline[] = ['$group' => $group];
     // $project the keys into the result
     $project = [];
     $project['_id'] = 0;
     foreach ($keys as $key => $value) {
         if (is_numeric($key)) {
             $project[$value] = '$_id';
         } else {
             $project[$key] = '$_id.' . $key;
         }
     }
     if (sizeof($count) == 0 && sizeof($sum) == 0) {
         $project['count'] = 1;
     }
     if (sizeof($count) > 0) {
         foreach ($count as $s) {
             $project[str_replace('.', '_', $s) . 'Count'] = 1;
         }
     }
     if (sizeof($sum) > 0) {
         foreach ($sum as $s) {
             $project[str_replace('.', '_', $s) . 'Sum'] = 1;
         }
     }
     $pipeline[] = ['$project' => $project];
     // Assign the sort to the pipeline
     if (sizeof($sort) > 0) {
         $pipeline[] = ['$sort' => $sort];
     }
     // And add the limit
     if ($limit != null) {
         $pipeline[] = ['$limit' => (int) $limit];
     }
     // Prep the cursor
     $mdb = new self();
     $collection = $mdb->getCollection($collection);
     if (!$debug) {
         MongoCursor::$timeout = -1;
     }
     // this should be deprecated but aggregate doesn't have a timeout
     // Execute the query
     $result = $collection->aggregate($pipeline);
     if ($result['ok'] == 1) {
         return $result['result'];
     }
     throw new Exception('pipeline query failure');
 }