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

Expr::references should go through discriminator map #1333

Merged
merged 4 commits into from
Jan 7, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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: 10 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public static function mappingNotFound($className, $fieldName)
return new self("No mapping found for field '$fieldName' in class '$className'.");
}

/**
* @param string $className
* @param string $fieldName
* @return MappingException
*/
public static function mappingNotFoundInClassNorDescendants($className, $fieldName)
{
return new self("No mapping found for field '$fieldName' in class '$className' nor its descendants.");
}

/**
* @param string $document
* @param string $fieldName
Expand Down
19 changes: 18 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;

/**
* Query expression builder for ODM.
Expand Down Expand Up @@ -60,7 +61,23 @@ public function setClassMetadata(ClassMetadata $class)
public function references($document)
{
if ($this->currentField) {
$mapping = $this->class->getFieldMapping($this->currentField);
try {
$mapping = $this->class->getFieldMapping($this->currentField);
} catch (MappingException $e) {
if (empty($this->class->discriminatorMap)) {
throw $e;
}
foreach ($this->class->discriminatorMap as $child) {
$childClass = $this->dm->getClassMetadata($child);
if ($childClass->hasAssociation($this->currentField)) {
$mapping = $childClass->getFieldMapping($this->currentField);
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than give up after finding the first mapping, I think we should check all discriminator possibilities and throw if we find two conflicting mappings. It should be sufficient to compare the mapping arrays with ===.

This would warn users about an edge case of two child classes defining conflicting mappings and remove the ambiguity of selecting the first one that happened to show up during our discriminator map scan.

}
}
if ( ! isset($mapping) || $mapping === null) {
throw MappingException::mappingNotFoundInClassNorDescendants($this->class->name, $this->currentField);
}
}
$dbRef = $this->dm->createDBRef($document, $mapping);

if (isset($mapping['simple']) && $mapping['simple']) {
Expand Down
67 changes: 65 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,79 @@

namespace Doctrine\ODM\MongoDB\Tests\Query;

use Doctrine\ODM\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Documents\Feature;

class BuilderTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
/**
* @expectedException InvalidArgumentException
* @expectedException \InvalidArgumentException
*/
public function testPrimeRequiresBooleanOrCallable()
{
$this->dm->createQueryBuilder('Documents\User')
->field('groups')->prime(1);
}

public function testReferencesGoesThroughDiscriminatorMap()
{
$f = new Feature('Smarter references');
$this->dm->persist($f);

$q1 = $this->dm->createQueryBuilder(ParentClass::class)
->field('featureFull')->references($f)
->getQuery()->debug();

$this->assertEquals([ 'featureFull.$id' => new \MongoId($f->id) ], $q1['query']);

$q2 = $this->dm->createQueryBuilder(ParentClass::class)
->field('featureSimple')->references($f)
->getQuery()->debug();

$this->assertEquals([ 'featureSimple' => new \MongoId($f->id) ], $q2['query']);
}

/**
* @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException
* @expectedExceptionMessage No mapping found for field 'nope' in class 'Doctrine\ODM\MongoDB\Tests\Query\ParentClass' nor its descendants.
*/
public function testReferencesThrowsSpecializedExceptionForDiscriminatedDocuments()
{
$f = new Feature('Smarter references');
$this->dm->persist($f);

$this->dm->createQueryBuilder(ParentClass::class)
->field('nope')->references($f)
->getQuery();
}
}

/**
* @ODM\Document
* @ODM\InheritanceType("SINGLE_COLLECTION")
* @ODM\DiscriminatorField(fieldName="type")
* @ODM\DiscriminatorMap({"ca"="ChildA", "cb"="ChildB"})
*/
class ParentClass
{
/** @ODM\Id */
public $id;
}

/**
* @ODM\Document
*/
class ChildA extends ParentClass
{
/** @ODM\ReferenceOne(targetDocument="Documents\Feature") */
public $featureFull;
}

/**
* @ODM\Document
*/
class ChildB extends ParentClass
{
/** @ODM\ReferenceOne(targetDocument="Documents\Feature", simple=true) */
public $featureSimple;
}