Skip to content

Throw MappingException if owning/inverse side of refs are missing targetDocument #1136

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,11 @@ public function mapField(array $mapping)
if (isset($mapping['reference']) && ! empty($mapping['simple']) && ! isset($mapping['targetDocument'])) {
throw MappingException::simpleReferenceRequiresTargetDocument($this->name, $mapping['fieldName']);
}

if (isset($mapping['reference']) && empty($mapping['targetDocument']) && empty($mapping['discriminatorMap']) &&
(isset($mapping['mappedBy']) || isset($mapping['inversedBy']))) {
throw MappingException::owningAndInverseReferencesRequireTargetDocument($this->name, $mapping['fieldName']);
}

if ($this->isEmbeddedDocument && isset($mapping['strategy']) &&
($mapping['strategy'] === 'atomicSet' || $mapping['strategy'] === 'atomicSetArray')) {
Expand Down
18 changes: 17 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,25 @@ public static function simpleReferenceRequiresTargetDocument($className, $fieldN
{
return new self("Target document must be specified for simple reference: $className::$fieldName");
}


/**
* @param string $strategy
* @param string $className
* @param string $fieldName
* @return MappingException
*/
public static function atomicCollectionStrategyNotAllowed($strategy, $className, $fieldName)
{
return new self("$strategy collection strategy can be used only in top level document, used in $className::$fieldName");
}

/**
* @param string $className
* @param string $fieldName
* @return MappingException
*/
public static function owningAndInverseReferencesRequireTargetDocument($className, $fieldName)
{
return new self("Target document must be specified for owning/inverse sides of reference: $className::$fieldName");
}
}
27 changes: 27 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,33 @@ public function testAtomicCollectionUpdateUsageInEmbeddedDocument()
'strategy' => 'atomicSet',
));
}

/**
* @dataProvider provideOwningAndInversedRefsNeedTargetDocument
* @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException
*/
public function testOwningAndInversedRefsNeedTargetDocument($config)
{
$config = array_merge($config, array(
'fieldName' => 'many',
'reference' => true,
'strategy' => 'atomicSet',
));

$cm = new ClassMetadataInfo('stdClass');
$cm->isEmbeddedDocument = true;
$cm->mapField($config);
}

public function provideOwningAndInversedRefsNeedTargetDocument()
{
return array(
array(array('type' => 'one', 'mappedBy' => 'post')),
array(array('type' => 'one', 'inversedBy' => 'post')),
array(array('type' => 'many', 'mappedBy' => 'post')),
array(array('type' => 'many', 'inversedBy' => 'post')),
);
}
}

class TestCustomRepositoryClass extends DocumentRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function testClassMetadataInstanceSerialization()
public function testOwningSideAndInverseSide()
{
$cm = new ClassMetadataInfo('Documents\User');
$cm->mapManyReference(array('fieldName' => 'articles', 'inversedBy' => 'user'));
$cm->mapManyReference(array('fieldName' => 'articles', 'targetDocument' => 'Documents\Article', 'inversedBy' => 'user'));
$this->assertTrue($cm->fieldMappings['articles']['isOwningSide']);

$cm = new ClassMetadataInfo('Documents\Article');
$cm->mapOneReference(array('fieldName' => 'user', 'mappedBy' => 'articles'));
$cm->mapOneReference(array('fieldName' => 'user', 'targetDocument' => 'Documents\User', 'mappedBy' => 'articles'));
$this->assertTrue($cm->fieldMappings['user']['isInverseSide']);
}

Expand Down