Doctrine\MongoDB\Query\Query::getIterator PHP Method

getIterator() public method

If the query type is not expected to return an Iterator, BadMethodCallException will be thrown before executing the query. Otherwise, the query will be executed and UnexpectedValueException will be thrown if {@link Query::execute()} does not return an Iterator.
See also: http://php.net/manual/en/iteratoraggregate.getiterator.php
public getIterator ( ) : Doctrine\MongoDB\Iterator
return Doctrine\MongoDB\Iterator
    public function getIterator()
    {
        switch ($this->query['type']) {
            case self::TYPE_FIND:
            case self::TYPE_GROUP:
            case self::TYPE_MAP_REDUCE:
            case self::TYPE_DISTINCT:
            case self::TYPE_GEO_NEAR:
                break;
            default:
                throw new BadMethodCallException('Iterator would not be returned for query type: ' . $this->query['type']);
        }
        if ($this->iterator === null) {
            $iterator = $this->execute();
            if (!$iterator instanceof Iterator) {
                throw new UnexpectedValueException('Iterator was not returned from executed query');
            }
            $this->iterator = $iterator;
        }
        return $this->iterator;
    }

Usage Example

Example #1
0
 /**
  * @dataProvider provideQueryTypesThatDoReturnAnIterator
  * @expectedException UnexpectedValueException
  */
 public function testGetIteratorShouldThrowExceptionAfterExecutingForTypesThatShouldReturnAnIteratorButDoNot($type, $method)
 {
     $collection = $this->getMockCollection();
     $collection->expects($this->once())->method($method)->will($this->returnValue(null));
     // Create a query array with any fields that may be expected to exist
     $queryArray = array('type' => $type, 'query' => array(), 'group' => array('keys' => array(), 'initial' => array(), 'reduce' => '', 'options' => array()), 'mapReduce' => array('map' => '', 'reduce' => '', 'out' => '', 'options' => array()), 'geoNear' => array('near' => array(), 'options' => array()), 'distinct' => 0);
     $query = new Query($collection, $queryArray, array());
     $query->getIterator();
 }