Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Fix querying fields in reference structures #1798

Merged
merged 2 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,15 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
? $this->dm->getClassMetadata($targetMapping['targetDocument'])
: null;

$fieldNames = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue);
if (empty($targetMapping['reference'])) {
$fieldNames = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue);
} else {
// No recursive processing for references as most probably somebody is querying DBRef or alike
if ($nextObjectProperty[0] !== '$' && in_array($targetMapping['storeAs'], [ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB, ClassMetadata::REFERENCE_STORE_AS_DB_REF])) {
$nextObjectProperty = '$' . $nextObjectProperty;
}
$fieldNames = [[$nextObjectProperty, $value]];
}

return array_map(function ($preparedTuple) use ($fieldName) {
list($key, $value) = $preparedTuple;
Expand Down
32 changes: 32 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use Documents\Article;
use Documents\CmsComment;
use Documents\Group;
use Documents\IndirectlyReferencedUser;
use Documents\Phonenumber;
use Documents\ReferenceUser;
use Documents\User;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use function array_values;
use function get_class;
use function iterator_to_array;
use function strtotime;

class QueryTest extends BaseTest
Expand Down Expand Up @@ -366,6 +369,35 @@ public function testQueryReferences()
$this->assertSame($user, $user2);
}

public function testNestedQueryReference()
{
$referencedUser = new User();
$referencedUser->setUsername('boo');
$phonenumber = new Phonenumber('6155139185');
$referencedUser->addPhonenumber($phonenumber);

$indirectlyReferencedUser = new IndirectlyReferencedUser();
$indirectlyReferencedUser->user = $referencedUser;

$user = new ReferenceUser();
$user->indirectlyReferencedUsers[] = $indirectlyReferencedUser;

$this->dm->persist($referencedUser);
$this->dm->persist($user);
$this->dm->flush();

$qb = $this->dm->createQueryBuilder('Documents\ReferenceUser');

$referencedUsersQuery = $qb
->field('indirectlyReferencedUsers.user.id')->equals(new ObjectId($referencedUser->getId()))
->getQuery();

$referencedUsers = iterator_to_array($referencedUsersQuery->execute(), false);

$this->assertCount(1, $referencedUsers);
$this->assertSame($user, $referencedUsers[0]);
}

public function testQueryWhereIn()
{
$qb = $this->dm->createQueryBuilder(User::class);
Expand Down
18 changes: 16 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,22 @@ public function testQueryWithMultipleEmbeddedDocumentsAndReference()
$query = $qb->getQuery();
$debug = $query->debug('query');

$this->assertArrayHasKey('eO.eO.e1.eO.eP.pO._id', $debug);
$this->assertEquals($identifier, $debug['eO.eO.e1.eO.eP.pO._id']);
$this->assertArrayHasKey('eO.eO.e1.eO.eP.pO.$id', $debug);
$this->assertEquals($identifier, $debug['eO.eO.e1.eO.eP.pO.$id']);
}

public function testQueryWithMultipleEmbeddedDocumentsAndReferenceUsingDollarSign()
{
$identifier = new ObjectId();

$qb = $this->dm->createQueryBuilder(__NAMESPACE__ . '\EmbedTest')
->find()
->field('embeddedOne.embeddedOne.embeddedMany.embeddedOne.pet.owner.$id')->equals((string) $identifier);
$query = $qb->getQuery();
$debug = $query->debug('query');

$this->assertArrayHasKey('eO.eO.e1.eO.eP.pO.$id', $debug);
$this->assertEquals($identifier, $debug['eO.eO.e1.eO.eP.pO.$id']);
}

public function testSelectVsSingleCollectionInheritance()
Expand Down
20 changes: 20 additions & 0 deletions tests/Documents/IndirectlyReferencedUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Documents;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* @ODM\EmbeddedDocument
*/
class IndirectlyReferencedUser
{
/**
* @var User
*
* @ODM\ReferenceOne(targetDocument="Documents\User", storeAs="ref")
*/
public $user;
}
7 changes: 7 additions & 0 deletions tests/Documents/ReferenceUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class ReferenceUser
*/
public $referencedUsers = [];

/**
* @ODM\EmbedMany(targetDocument="Documents\IndirectlyReferencedUser")
*
* @var IndirectlyReferencedUser[]
*/
public $indirectlyReferencedUsers = [];

/**
* @ODM\Field(type="string")
*
Expand Down