Skip to content

Commit

Permalink
Merge pull request #468 from dantleech/referrer_field_query
Browse files Browse the repository at this point in the history
Supported for using referrer fields in queries
  • Loading branch information
lsmith77 committed Apr 8, 2014
2 parents 12bb5c7 + ae54871 commit 5d2e990
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
14 changes: 13 additions & 1 deletion lib/Doctrine/ODM/PHPCR/Query/Builder/BuilderConverterPhpcr.php
Expand Up @@ -122,7 +122,19 @@ protected function getMetadata($alias)
*/
protected function getPhpcrProperty($originalAlias, $odmField)
{
$fieldMeta = $this->getMetadata($originalAlias)->getField($odmField);
$meta = $this->getMetadata($originalAlias);

if ($meta->hasField($odmField)) {
$fieldMeta = $meta->getField($odmField);
} elseif ($meta->hasAssociation($odmField)) {
$fieldMeta = $meta->getAssociation($odmField);
} else {
throw new \Exception(sprintf(
'Could not find a mapped field or association named "%s" for alias "%s"',
$odmField, $originalAlias
));
}

$propertyName = $fieldMeta['property'];

if (empty($fieldMeta['translated'])
Expand Down
5 changes: 5 additions & 0 deletions tests/Doctrine/Tests/Models/CMS/CmsAddress.php
Expand Up @@ -28,6 +28,11 @@ class CmsAddress
/** @PHPCRODM\ReferenceOne(targetDocument="CmsUser") */
public $user;

/**
* @PHPCRODM\Uuid
*/
public $uuid;

public function getId()
{
return $this->id;
Expand Down
35 changes: 19 additions & 16 deletions tests/Doctrine/Tests/ODM/PHPCR/Functional/QueryBuilderJoinTest.php
Expand Up @@ -23,19 +23,26 @@ public function setUp()

$this->resetFunctionalNode($this->dm);

$address = new CmsAddress;
$address->country = 'France';
$address->city = 'Lyon';
$address->zip = '65019';
$this->dm->persist($address);
$address1 = new CmsAddress;
$address1->country = 'France';
$address1->city = 'Lyon';
$address1->zip = '65019';
$this->dm->persist($address1);

$address2 = new CmsAddress;
$address2->country = 'England';
$address2->city = 'Weymouth';
$address2->zip = 'AB1DC2';
$this->dm->persist($address2);

$user = new CmsUser;
$user->username = 'dantleech';
$user->address = $address;
$user->address = $address1;
$this->dm->persist($user);

$user = new CmsUser;
$user->username = 'winstonsmith';
$user->address = $address2;
$this->dm->persist($user);

$user = new CmsUser;
Expand All @@ -53,23 +60,19 @@ public function setUp()
$this->dm->flush();
}

/**
* This doesn't work -- I didn't really expect it to, but is it possible
* to join a reference?
*/
public function testEquiJoinInnerOnReference()
{
$qb = $this->dm->createQueryBuilder();
$qb->fromDocument('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$qb->addJoinInner()
->right()->document('Doctrine\Tests\Models\CMS\CmsAddress', 'a')->end()
->condition()->equi('u.address', 'a.uuid');
try {
$q = $qb->getQuery();
$res = $q->execute();
} catch (\Exception $e) {
$this->markTestSkipped('How do we join a reference? Exception: '.$e->getMessage());
}
$qb->where()->eq()->field('a.city')->literal('Lyon');
$q = $qb->getQuery();
$res = $q->execute();

$this->assertCount(1, $res);
$this->assertEquals('dantleech', $res->current()->username);
}

public function provideEquiJoinInner()
Expand Down
Expand Up @@ -39,6 +39,9 @@ public function setUp()
return $meta;
}

$meta->expects($me->any())
->method('hasField')
->will($me->returnValue(true));

$meta->expects($me->any())
->method('getField')
Expand Down

0 comments on commit 5d2e990

Please sign in to comment.