Skip to content
This repository has been archived by the owner on May 31, 2018. It is now read-only.

Commit

Permalink
added group support to Query Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
navruzm committed Mar 23, 2013
1 parent ff22d53 commit 6c8da0b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/LMongo/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,61 @@ public function aggregate($function, $column, $query = array())
return 0;
}

/**
* Execute a group command on the database.
*
* @param array $initial
* @param mixed $reduce
* @param mixed $columns
* @param array $options
* @return LMongo\Query\Cursor
*/
public function group(array $initial, $reduce, $columns = array(), array $options = array())
{
if (is_null($this->columns))
{
$this->columns = $columns;
}

if (is_string($this->columns))
{
$this->columns = new \MongoCode($this->columns);
}

if (is_string($reduce))
{
$reduce = new \MongoCode($reduce);
}

$conditions = $this->compileWheres($this);

if(count($conditions))
{
$options['condition'] = $conditions;
}

if (isset($options['finalize']) and is_string($options['finalize']))
{
$options['finalize'] = new \MongoCode($options['finalize']);
}

if(empty($options))
{
$result = $this->connection->{$this->collection}->group($this->columns, $initial, $reduce);
}
else
{
$result = $this->connection->{$this->collection}->group($this->columns, $initial, $reduce, $options);
}

if ( ! $result['ok'])
{
throw new \MongoException($result['errmsg']);
}

return $result['retval'];
}

/**
* Insert a new document into the database.
*
Expand Down
14 changes: 12 additions & 2 deletions tests/LMongoQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,16 @@ public function testTruncateMethod()
$this->assertEquals(true, $result);
}

public function testGroupMethod()
{
$this->insertData();
$builder = $this->getBuilder();
$initial = array('count' => 0);
$reduce = 'function (obj, prev) { prev.count++; }';
$result = $builder->collection('test')->whereGt('no', 2)->group($initial, $reduce);
$this->assertEquals(2, $result[0]['count']);
}

public function testPaginateCorrectlyCreatesPaginatorInstance()
{
$connection = m::mock('LMongo\Connection');
Expand All @@ -692,10 +702,10 @@ public function testPaginateCorrectlyCreatesPaginatorInstance()
$cursor->shouldReceive('countAll')->once()->andReturn(10);
$cursor->shouldReceive('toArray')->once()->andReturn(array('foo'));
$builder->expects($this->once())->method('forPage')->with($this->equalTo(1), $this->equalTo(15))->will($this->returnValue($builder));
$builder->expects($this->once())->method('get')->with($this->equalTo(array('*')))->will($this->returnValue($cursor));
$builder->expects($this->once())->method('get')->with($this->equalTo(array()))->will($this->returnValue($cursor));
$paginator->shouldReceive('make')->once()->with(array('foo'), 10, 15)->andReturn(array('results'));

$this->assertEquals(array('results'), $builder->paginate(15, array('*')));
$this->assertEquals(array('results'), $builder->paginate(15, array()));
}

protected function insertData()
Expand Down

1 comment on commit 6c8da0b

@duong1008
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to use count group ??? ,thanks

Please sign in to comment.