Skip to content

Commit

Permalink
Merge pull request doctrine#1406 from coudenysj/bugfix/elemmatch-need…
Browse files Browse the repository at this point in the history
…s-an-object-1.0.x

Fix the ' needs an Object' exception for simple references
  • Loading branch information
alcaeus committed May 11, 2016
2 parents bccb028 + 2a9bd19 commit c7ec70d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function includesReferenceTo($document)
$dbRef = $this->dm->createDBRef($document, $mapping);

if (isset($mapping['simple']) && $mapping['simple']) {
$this->query[$mapping['name']]['$elemMatch'] = $dbRef;
$this->query[$mapping['name']] = $dbRef;
} else {
$keys = array('ref' => true, 'id' => true, 'db' => true);

Expand Down
61 changes: 61 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ public function testReferences()
$this->assertSame($kris, $query->getSingleResult());
}

public function testReferencesSimple()
{
$kris = new Person('Kris');
$jon = new Person('Jon');

$this->dm->persist($kris);
$this->dm->persist($jon);
$this->dm->flush();

$kris->bestFriendSimple = $jon;
$this->dm->flush();

$qb = $this->dm->createQueryBuilder(__NAMESPACE__.'\Person');
$qb->field('bestFriendSimple')->references($jon);

$queryArray = $qb->getQueryArray();
$this->assertEquals(array(
'bestFriendSimple' => new \MongoId($jon->id),
), $queryArray);

$query = $qb->getQuery();

$this->assertEquals(1, $query->count());
$this->assertSame($kris, $query->getSingleResult());
}

public function testIncludesReferenceTo()
{
$kris = new Person('Kris');
Expand Down Expand Up @@ -105,6 +131,35 @@ public function testIncludesReferenceTo()
$this->assertSame($jon, $query->getSingleResult());
}

public function testIncludesReferenceToSimple()
{
$kris = new Person('Kris');
$jon = new Person('Jon');
$jachim = new Person('Jachim');

$this->dm->persist($kris);
$this->dm->persist($jon);
$this->dm->persist($jachim);
$this->dm->flush();

$jon->friendsSimple[] = $kris;
$jon->friendsSimple[] = $jachim;
$this->dm->flush();

$qb = $this->dm->createQueryBuilder(__NAMESPACE__.'\Person');
$qb->field('friendsSimple')->includesReferenceTo($kris);

$queryArray = $qb->getQueryArray();
$this->assertEquals(array(
'friendsSimple' => new \MongoId($kris->id)
), $queryArray);

$query = $qb->getQuery();

$this->assertEquals(1, $query->count());
$this->assertSame($jon, $query->getSingleResult());
}

public function testQueryIdIn()
{
$user = new \Documents\User();
Expand Down Expand Up @@ -251,9 +306,15 @@ class Person
/** @ODM\ReferenceOne */
public $bestFriend;

/** @ODM\ReferenceOne(simple=true, targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */
public $bestFriendSimple;

/** @ODM\ReferenceMany */
public $friends = array();

/** @ODM\ReferenceMany(simple=true, targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */
public $friendsSimple = array();

public function __construct($firstName)
{
$this->firstName = $firstName;
Expand Down

0 comments on commit c7ec70d

Please sign in to comment.