public function execute()
 {
     switch ($this->query['type']) {
         case self::TYPE_FIND:
             if (isset($this->query['mapReduce']['reduce'])) {
                 $this->query['query'][$this->cmd . 'where'] = $this->query['mapReduce']['reduce'];
             }
             $cursor = $this->collection->find($this->query['query'], $this->query['select']);
             $this->prepareCursor($cursor);
             return $cursor;
         case self::TYPE_FIND_AND_UPDATE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             if ($this->query['upsert']) {
                 $this->options['upsert'] = true;
             }
             if ($this->query['new']) {
                 $this->options['new'] = true;
             }
             return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_FIND_AND_REMOVE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             return $this->collection->findAndRemove($this->query['query'], $this->options);
         case self::TYPE_INSERT:
             return $this->collection->insert($this->query['newObj']);
         case self::TYPE_UPDATE:
             return $this->collection->update($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_REMOVE:
             return $this->collection->remove($this->query['query'], $this->options);
         case self::TYPE_GROUP:
             return $this->collection->group($this->query['group']['keys'], $this->query['group']['initial'], $this->query['mapReduce']['reduce'], $this->query['query']);
         case self::TYPE_MAP_REDUCE:
             if (!isset($this->query['mapReduce']['out'])) {
                 $this->query['mapReduce']['out'] = array('inline' => true);
             }
             $cursor = $this->collection->mapReduce($this->query['mapReduce']['map'], $this->query['mapReduce']['reduce'], $this->query['mapReduce']['out'], $this->query['query'], $this->options);
             if ($cursor instanceof Cursor) {
                 $this->prepareCursor($cursor);
             }
             return $cursor;
         case self::TYPE_DISTINCT_FIELD:
             return $this->collection->distinct($this->query['distinctField'], $this->query['query'], $this->options);
         case self::TYPE_GEO_LOCATION:
             if (isset($this->query['limit']) && $this->query['limit']) {
                 $this->options['num'] = $this->query['limit'];
             }
             return $this->collection->near($this->query['near'], $this->query['query'], $this->options);
     }
 }
 /**
  * @see Collection::findAndUpdate()
  */
 public function findAndUpdate(array $query, array $newObj, array $options = [])
 {
     $this->log(['findAndUpdate' => true, 'query' => $query, 'newObj' => $newObj, 'options' => $options]);
     return parent::findAndUpdate($query, $newObj, $options);
 }
Beispiel #3
0
 /**
  * Execute the query and return its result.
  *
  * The return value will vary based on the query type. Commands with results
  * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other
  * commands and operations may return a status array or a boolean, depending
  * on the driver's write concern. Queries and some mapReduce commands will
  * return a CursorInterface.
  *
  * @return mixed
  */
 public function execute()
 {
     $options = $this->options;
     switch ($this->query['type']) {
         case self::TYPE_FIND:
             $cursor = $this->collection->find($this->query['query'], isset($this->query['select']) ? $this->query['select'] : array());
             return $this->prepareCursor($cursor);
         case self::TYPE_FIND_AND_UPDATE:
             $queryOptions = $this->getQueryOptions('new', 'select', 'sort', 'upsert');
             $queryOptions = $this->renameQueryOptions($queryOptions, array('select' => 'fields'));
             return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], array_merge($options, $queryOptions));
         case self::TYPE_FIND_AND_REMOVE:
             $queryOptions = $this->getQueryOptions('select', 'sort');
             $queryOptions = $this->renameQueryOptions($queryOptions, array('select' => 'fields'));
             return $this->collection->findAndRemove($this->query['query'], array_merge($options, $queryOptions));
         case self::TYPE_INSERT:
             return $this->collection->insert($this->query['newObj'], $options);
         case self::TYPE_UPDATE:
             return $this->collection->update($this->query['query'], $this->query['newObj'], array_merge($options, $this->getQueryOptions('multiple', 'upsert')));
         case self::TYPE_REMOVE:
             return $this->collection->remove($this->query['query'], $options);
         case self::TYPE_GROUP:
             if (!empty($this->query['query'])) {
                 $options['cond'] = $this->query['query'];
             }
             $collection = $this->collection;
             $query = $this->query;
             $closure = function () use($collection, $query, $options) {
                 return $collection->group($query['group']['keys'], $query['group']['initial'], $query['group']['reduce'], array_merge($options, $query['group']['options']));
             };
             return $this->withReadPreference($collection->getDatabase(), $closure);
         case self::TYPE_MAP_REDUCE:
             if (isset($this->query['limit'])) {
                 $options['limit'] = $this->query['limit'];
             }
             $collection = $this->collection;
             $query = $this->query;
             $closure = function () use($collection, $query, $options) {
                 return $collection->mapReduce($query['mapReduce']['map'], $query['mapReduce']['reduce'], $query['mapReduce']['out'], $query['query'], array_merge($options, $query['mapReduce']['options']));
             };
             $results = $this->withReadPreference($collection->getDatabase(), $closure);
             return $results instanceof Cursor ? $this->prepareCursor($results) : $results;
         case self::TYPE_DISTINCT:
             $collection = $this->collection;
             $query = $this->query;
             $closure = function () use($collection, $query, $options) {
                 return $collection->distinct($query['distinct'], $query['query'], $options);
             };
             return $this->withReadPreference($collection->getDatabase(), $closure);
         case self::TYPE_GEO_NEAR:
             if (isset($this->query['limit'])) {
                 $options['num'] = $this->query['limit'];
             }
             $collection = $this->collection;
             $query = $this->query;
             $closure = function () use($collection, $query, $options) {
                 return $collection->near($query['geoNear']['near'], $query['query'], array_merge($options, $query['geoNear']['options']));
             };
             return $this->withReadPreference($collection->getDatabase(), $closure);
         case self::TYPE_COUNT:
             $collection = $this->collection;
             $query = $this->query;
             $closure = function () use($collection, $query) {
                 return $collection->count($query['query']);
             };
             return $this->withReadPreference($collection, $closure);
     }
 }
Beispiel #4
0
 public function execute()
 {
     switch ($this->query['type']) {
         case self::TYPE_FIND:
             $cursor = $this->collection->find($this->query['query'], $this->query['select']);
             return $this->prepareCursor($cursor);
         case self::TYPE_FIND_AND_UPDATE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             if ($this->query['upsert']) {
                 $this->options['upsert'] = true;
             }
             if ($this->query['new']) {
                 $this->options['new'] = true;
             }
             return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_FIND_AND_REMOVE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             return $this->collection->findAndRemove($this->query['query'], $this->options);
         case self::TYPE_INSERT:
             return $this->collection->insert($this->query['newObj']);
         case self::TYPE_UPDATE:
             if ($this->query['upsert']) {
                 $this->options['upsert'] = $this->query['upsert'];
             }
             if ($this->query['multiple']) {
                 $this->options['multiple'] = $this->query['multiple'];
             }
             return $this->collection->update($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_REMOVE:
             return $this->collection->remove($this->query['query'], $this->options);
         case self::TYPE_GROUP:
             if (!empty($this->query['query'])) {
                 $this->query['group']['options']['condition'] = $this->query['query'];
             }
             $options = array_merge($this->options, $this->query['group']['options']);
             return $this->collection->group($this->query['group']['keys'], $this->query['group']['initial'], $this->query['group']['reduce'], $options);
         case self::TYPE_MAP_REDUCE:
             if (!isset($this->query['mapReduce']['out'])) {
                 $this->query['mapReduce']['out'] = array('inline' => true);
             }
             $options = array_merge($this->options, $this->query['mapReduce']['options']);
             $cursor = $this->collection->mapReduce($this->query['mapReduce']['map'], $this->query['mapReduce']['reduce'], $this->query['mapReduce']['out'], $this->query['query'], $options);
             if ($cursor instanceof Cursor) {
                 $cursor = $this->prepareCursor($cursor);
             }
             return $cursor;
         case self::TYPE_DISTINCT_FIELD:
             return $this->collection->distinct($this->query['distinctField'], $this->query['query'], $this->options);
         case self::TYPE_GEO_LOCATION:
             if (isset($this->query['limit']) && $this->query['limit']) {
                 $this->options['num'] = $this->query['limit'];
             }
             foreach (array('distanceMultiplier', 'maxDistance', 'spherical') as $key) {
                 if (isset($this->query['geoNear'][$key]) && $this->query['geoNear'][$key]) {
                     $this->options[$key] = $this->query['geoNear'][$key];
                 }
             }
             return $this->collection->near($this->query['geoNear']['near'], $this->query['query'], $this->options);
         case self::TYPE_COUNT:
             return $this->collection->count($this->query['query']);
     }
 }