diff --git a/CHANGELOG.md b/CHANGELOG.md index 097943c23e..dd1926eb41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ a release. ## [Unreleased] ### Changed - Add conflict with "doctrine/orm" >=2.16 (temporary change until code is fixed) +### Fixed +- References: fixed condition in `XML` Driver that did not allow to retrieve from the entity definition the `mappedBy` and `inversedBy` fields. ### Fixed - Fix bug collecting metadata for inherited mapped classes diff --git a/schemas/orm/doctrine-extensions-mapping-2-2.xsd b/schemas/orm/doctrine-extensions-mapping-2-2.xsd index 34ce2a37ed..71f30049a1 100644 --- a/schemas/orm/doctrine-extensions-mapping-2-2.xsd +++ b/schemas/orm/doctrine-extensions-mapping-2-2.xsd @@ -29,6 +29,7 @@ + @@ -65,6 +66,7 @@ + diff --git a/src/References/Mapping/Driver/Xml.php b/src/References/Mapping/Driver/Xml.php index 9a7b0c774d..8f5bd0954a 100644 --- a/src/References/Mapping/Driver/Xml.php +++ b/src/References/Mapping/Driver/Xml.php @@ -93,10 +93,10 @@ public function readExtendedMetadata($meta, array &$config) 'identifier' => $identifier, ]; - if (!$this->_isAttributeSet($element, 'mappedBy')) { + if ($this->_isAttributeSet($element, 'mappedBy')) { $config[$reference][$field]['mappedBy'] = $this->_getAttribute($element, 'mappedBy'); } - if (!$this->_isAttributeSet($element, 'inversedBy')) { + if ($this->_isAttributeSet($element, 'inversedBy')) { $config[$reference][$field]['inversedBy'] = $this->_getAttribute($element, 'inversedBy'); } } diff --git a/tests/Gedmo/Mapping/Driver/Xml/Gedmo.Tests.Mapping.Fixture.Xml.References.dcm.xml b/tests/Gedmo/Mapping/Driver/Xml/Gedmo.Tests.Mapping.Fixture.Xml.References.dcm.xml new file mode 100644 index 0000000000..32c8e2c6a9 --- /dev/null +++ b/tests/Gedmo/Mapping/Driver/Xml/Gedmo.Tests.Mapping.Fixture.Xml.References.dcm.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tests/Gedmo/Mapping/Fixture/Xml/References.php b/tests/Gedmo/Mapping/Fixture/Xml/References.php new file mode 100644 index 0000000000..d18e62393c --- /dev/null +++ b/tests/Gedmo/Mapping/Fixture/Xml/References.php @@ -0,0 +1,32 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tests\Mapping\Fixture\Xml; + +use Gedmo\Tests\Mapping\Fixture\Document\User; + +class References +{ + /** + * @var int + */ + private $id; + + /** + * @var string + */ + private $name; + + /** + * @var User[] + */ + private $users; +} diff --git a/tests/Gedmo/Mapping/Xml/ReferencesMappingTest.php b/tests/Gedmo/Mapping/Xml/ReferencesMappingTest.php new file mode 100644 index 0000000000..29590c099a --- /dev/null +++ b/tests/Gedmo/Mapping/Xml/ReferencesMappingTest.php @@ -0,0 +1,80 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tests\Mapping\Xml; + +use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\Common\EventManager; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Mapping\Driver\AnnotationDriver; +use Doctrine\ORM\Mapping\Driver\XmlDriver; +use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; +use Gedmo\References\ReferencesListener; +use Gedmo\Tests\Mapping\Fixture\Xml\References; +use Gedmo\Tests\Tool\BaseTestCaseOM; + +/** + * @author Guillermo Fuentes + */ +final class ReferencesMappingTest extends BaseTestCaseOM +{ + /** + * @var EntityManager + */ + private $em; + + /** + * @var ReferencesListener + */ + private $referencesListener; + + protected function setUp(): void + { + parent::setUp(); + + $reader = new AnnotationReader(); + $annotationDriver = new AnnotationDriver($reader); + + $xmlDriver = new XmlDriver(__DIR__.'/../Driver/Xml'); + + $chain = new MappingDriverChain(); + $chain->addDriver($xmlDriver, 'Gedmo\Tests\Mapping\Fixture\Xml'); + $chain->addDriver($annotationDriver, 'Gedmo\Tests\Mapping\Fixture'); + + $this->referencesListener = new ReferencesListener(); + $this->evm = new EventManager(); + $this->evm->addEventSubscriber($this->referencesListener); + + $this->em = $this->getDefaultMockSqliteEntityManager([ + References::class, + ], $chain); + } + + public function testMetadata(): void + { + $meta = $this->em->getClassMetadata(References::class); + $config = $this->referencesListener->getConfiguration($this->em, $meta->getName()); + + static::assertArrayHasKey('referenceMany', $config); + static::assertArrayHasKey('useObjectClass', $config); + static::assertSame(References::class, $config['useObjectClass']); + $configInternal = $config['referenceMany']; + static::assertArrayHasKey('users', $configInternal); + $configUsers = $configInternal['users']; + static::assertArrayHasKey('field', $configUsers); + static::assertArrayHasKey('type', $configUsers); + static::assertSame('document', $configUsers['type']); + static::assertArrayHasKey('class', $configUsers); + static::assertArrayHasKey('identifier', $configUsers); + static::assertArrayHasKey('mappedBy', $configUsers); + static::assertSame('reference', $configUsers['mappedBy']); + } +}