Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
Support $maxDistance on $near query field criteria
Browse files Browse the repository at this point in the history
Since maxDistance() was already used for the geoNear option with the same name, the method is overloaded based on whether geoNear() has already been called. Fixes #85.
  • Loading branch information
jmikola committed Dec 8, 2012
1 parent 150f740 commit 59f73cd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/Doctrine/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,23 @@ public function distanceMultiplier($distanceMultiplier)
}

/**
* Set the "maxDistance" option for a geoNear command query.
* Set the "maxDistance" option for a geoNear command query or add
* $maxDistance criteria to the query.
*
* If the query type is geospatial (i.e. geoNear() was called), the
* "maxDistance" command option will be set; otherwise, $maxDistance will be
* added to the current expression.
*
* @param string $maxDistance
* @return Builder
*/
public function maxDistance($maxDistance)
{
$this->query['geoNear']['maxDistance'] = $maxDistance;
if (Query::TYPE_GEO_LOCATION === $this->query['type']) {
$this->query['geoNear']['maxDistance'] = $maxDistance;
} else {
$this->expr->maxDistance($maxDistance);
}
return $this;
}

Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ public function near($x, $y)
return $this;
}

public function maxDistance($maxDistance)
{
if ($this->currentField) {
$this->query[$this->currentField][$this->cmd . 'maxDistance'] = $maxDistance;
} else {
$this->query[$this->cmd . 'maxDistance'] = $maxDistance;
}
return $this;
}

public function withinBox($x1, $y1, $x2, $y2)
{
if ($this->currentField) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ public function testGeoNearQuery()
public function testNear()
{
$qb = $this->getTestQueryBuilder()
->field('loc')->near(50, 50);
->field('loc')->near(50, 50)->maxDistance(25);

$expected = array('loc' => array('$near' => array(50, 50)));
$expected = array('loc' => array('$near' => array(50, 50), '$maxDistance' => 25));
$this->assertEquals($expected, $qb->getQueryArray());
}

Expand Down

0 comments on commit 59f73cd

Please sign in to comment.