Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix Builder near() #53

Closed
wants to merge 1 commit into from

3 participants

@madapaja

fix issue #47
#47

@jmikola jmikola commented on the diff
tests/Doctrine/MongoDB/Tests/Query/BuilderTest.php
@@ -92,15 +92,20 @@ public function testFindAndUpdateQuery()
public function testGeoLocationQuery()
{
$qb = $this->getTestQueryBuilder()
- ->field('x')->near(1)
- ->field('y')->near(2)
+ ->near(1, 2)
@jmikola Owner
jmikola added a note

Shouldn't near() still operate on a field? This doesn't seem to agree with the Geospatial Indexing docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwage
Owner

This doesn't seem right.

@jmikola
Owner

@jwage: I took another look at this. I think the issue is confusing the $near query with the geoNear command. The query builder interface lead me to assume that we were doing the former, but it turns out the built Query object executes this instead:

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);

The Collection::near() method issues a geoNear() command. I think this should probably be changed to use $near and perhaps we can create a new method like geoNear() to trigger command behavior.

@jmikola
Owner

I fixed this a bit differently in #63.

@madapaja: I attributed a commit to you for the preNear event arg fix. Thanks!

@jmikola jmikola closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 1, 2012
  1. Fix Builder near()

    Koji Iwazaki authored
This page is out of date. Refresh to see the latest.
View
2  lib/Doctrine/MongoDB/Collection.php
@@ -330,7 +330,7 @@ protected function doFindAndUpdate(array $query, array $newObj, array $options)
public function near(array $near, array $query = array(), array $options = array())
{
if ($this->eventManager->hasListeners(Events::preNear)) {
- $this->eventManager->dispatchEvent(Events::preNear, new NearEventArgs($this, $near, $query));
+ $this->eventManager->dispatchEvent(Events::preNear, new NearEventArgs($this, $query, $near));
}
$result = $this->doNear($near, $query, $options);
View
8 lib/Doctrine/MongoDB/Query/Builder.php
@@ -356,14 +356,14 @@ public function selectSlice($fieldName, $skip, $limit = null)
/**
* Add where near criteria.
*
- * @param string $x
- * @param string $y
+ * @param float $x
+ * @param float $y
* @return Builder
*/
- public function near($value)
+ public function near($x, $y)
{
$this->query['type'] = Query::TYPE_GEO_LOCATION;
- $this->query['near'][$this->currentField] = $value;
+ $this->query['near'] = array($x, $y);
return $this;
}
View
9 tests/Doctrine/MongoDB/Tests/Query/BuilderTest.php
@@ -92,15 +92,20 @@ public function testFindAndUpdateQuery()
public function testGeoLocationQuery()
{
$qb = $this->getTestQueryBuilder()
- ->field('x')->near(1)
- ->field('y')->near(2)
+ ->near(1, 2)
@jmikola Owner
jmikola added a note

Shouldn't near() still operate on a field? This doesn't seem to agree with the Geospatial Indexing docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
->field('username')->equals('jwage');
$this->assertEquals(Query::TYPE_GEO_LOCATION, $qb->getType());
+
$expected = array(
'username' => 'jwage'
);
$this->assertEquals($expected, $qb->getQueryArray());
+
+ $expected = array(1, 2);
+ $query = $qb->getQuery()->getQuery();
+ $this->assertEquals($expected, $query['near']);
+
$this->assertInstanceOf('Doctrine\MongoDB\ArrayIterator', $qb->getQuery()->execute());
}
Something went wrong with that request. Please try again.