Skip to content

Commit

Permalink
Fix bug-doctrine#9536
Browse files Browse the repository at this point in the history
Wrong validation message is displayed when an incorrect bidirectional
bi-directional mapping is set up. When the owning side is configured
correctly and the target side is missing the back reference, the ORM
suggests adding inverseBy instead of mappedBy, with the field name
missing. This commit fixes this problem.
  • Loading branch information
kiler129 committed Feb 22, 2022
1 parent 193c3ab commit 79f73a2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public function validateClass(ClassMetadataInfo $class)
'field ' . $assoc['targetEntity'] . '#' . $assoc['inversedBy'] . ' which does not exist.';
} elseif ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] === null) {
$ce[] = 'The field ' . $class->name . '#' . $fieldName . ' is on the owning side of a ' .
'bi-directional relationship, but the specified mappedBy association on the target-entity ' .
$assoc['targetEntity'] . '#' . $assoc['mappedBy'] . ' does not contain the required ' .
"'inversedBy' attribute.";
'bi-directional relationship, but the specified inversedBy association on the target-entity ' .
$assoc['targetEntity'] . '#' . $assoc['inversedBy'] . ' does not contain the required ' .
"'mappedBy=\"" . $fieldName . "\"' attribute.";
} elseif ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] !== $fieldName) {
$ce[] = 'The mappings ' . $class->name . '#' . $fieldName . ' and ' .
$assoc['targetEntity'] . '#' . $assoc['inversedBy'] . ' are ' .
Expand Down
59 changes: 59 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/SchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ public function testInvalidBiDirectionalRelationMappingMissingInversedByAttribut
);
}

/**
* @group 9536
*/
public function testInvalidBiDirectionalRelationMappingMissingMappedByAttribute(): void
{
$class = $this->em->getClassMetadata(Issue9536Owner::class);
$ce = $this->validator->validateClass($class);

self::assertEquals(
[
'The field Doctrine\Tests\ORM\Tools\Issue9536Owner#one is on the owning side of a bi-directional ' .
'relationship, but the specified inversedBy association on the target-entity ' .
"Doctrine\Tests\ORM\Tools\Issue9536Target#two does not contain the required 'mappedBy=\"one\"' " .
'attribute.',
],
$ce
);
}

/**
* @group DDC-3322
*/
Expand Down Expand Up @@ -435,6 +454,46 @@ class DDC3274Two
private $one;
}

/**
* @Entity
*/
class Issue9536Target
{
/**
* @var mixed
* @Id
* @Column
* @GeneratedValue
*/
private $id;

/**
* @var Issue9536Owner
* @OneToOne(targetEntity="Issue9536Owner")
*/
private $two;
}

/**
* @Entity
*/
class Issue9536Owner
{
/**
* @var mixed
* @Id
* @Column
* @GeneratedValue
*/
private $id;

/**
* @var Issue9536Target
* @OneToOne(targetEntity="Issue9536Target", inversedBy="two")
*/
private $one;
}

/**
* @Entity
*/
Expand Down

0 comments on commit 79f73a2

Please sign in to comment.