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

Add support for Doctrine DBAL 4 and ORM 3 #494

Merged
merged 7 commits into from
Feb 19, 2024
Merged
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"homepage": "https://github.com/sonata-project/sonata-doctrine-extensions",
"require": {
"php": "^8.0",
"doctrine/dbal": "^3.4",
"doctrine/dbal": "^3.4 || ^4.0",
"doctrine/persistence": "^3.0.2"
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-bundle": "^2.10",
"doctrine/event-manager": "^1.2 || ^2.0",
"doctrine/mongodb-odm": "^2.4",
"doctrine/orm": "^2.14",
"doctrine/orm": "^2.16 || ^3.0",
"friendsofphp/php-cs-fixer": "^3.4",
"matthiasnoback/symfony-config-test": "^4.2 || ^5.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.2.1 || ^5.0",
Expand All @@ -50,7 +50,7 @@
"conflict": {
"doctrine/doctrine-bundle": "<2.7",
"doctrine/mongodb-odm": "<2.4",
"doctrine/orm": "<2.14"
"doctrine/orm": "<2.16"
},
"suggest": {
"doctrine/mongodb-odm": "If you use mongo",
Expand Down
12 changes: 11 additions & 1 deletion src/Mapper/ORM/DoctrineORMMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,18 @@ private function loadDiscriminatorColumns(ClassMetadata $metadata): void
if (isset($this->discriminatorColumns[$metadata->getName()])) {
$arrayDiscriminatorColumns = $this->discriminatorColumns[$metadata->getName()];
if (isset($metadata->discriminatorColumn)) {
$arrayDiscriminatorColumns = array_merge($metadata->discriminatorColumn, $this->discriminatorColumns[$metadata->getName()]);
$arrayDiscriminatorColumns = array_merge(
(array) $metadata->discriminatorColumn,
$this->discriminatorColumns[$metadata->getName()]
);
}

/**
* ignored until https://github.com/doctrine/orm/pull/11226 is merged.
*
* @psalm-suppress InvalidArgument
* @phpstan-ignore-next-line
*/
$metadata->setDiscriminatorColumn($arrayDiscriminatorColumns);
}
} catch (\ReflectionException $e) {
Expand Down
1 change: 1 addition & 0 deletions tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
$container->loadFromExtension('doctrine', [
'dbal' => ['url' => 'sqlite://:memory:'],
'orm' => [
'report_fields_where_declared' => true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

needed on ORM 3. Otherwise:

1) Sonata\Doctrine\Tests\Mapper\ORM\DoctrineORMMapperTest::testDoctrineMappingLoaded
InvalidArgumentException: The $reportFieldsWhereDeclared argument is no longer supported, make sure to omit it when calling Doctrine\ORM\Mapping\Driver\AttributeDriver::__construct.

/var/www/sonata-doctrine-extensions/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php:45
/tmp/sonata-doctrine-extensions/var/cache/ContainerKqO7EY2/get_Doctrine_Orm_DefaultMetadataDriverService.php:21
/tmp/sonata-doctrine-extensions/var/cache/ContainerKqO7EY2/Sonata_Doctrine_Tests_App_KernelTestDebugContainer.php:98
/tmp/sonata-doctrine-extensions/var/cache/ContainerKqO7EY2/getDoctrine_Orm_DefaultConfigurationService.php:26
/tmp/sonata-doctrine-extensions/var/cache/ContainerKqO7EY2/Sonata_Doctrine_Tests_App_KernelTestDebugContainer.php:98
/tmp/sonata-doctrine-extensions/var/cache/ContainerKqO7EY2/getDoctrine_Orm_DefaultEntityManagerService.php:24
/tmp/sonata-doctrine-extensions/var/cache/ContainerKqO7EY2/getDoctrine_Orm_DefaultEntityManagerService.php:21
/var/www/sonata-doctrine-extensions/vendor/symfony/var-exporter/Internal/LazyObjectState.php:97
/var/www/sonata-doctrine-extensions/vendor/symfony/var-exporter/LazyGhostTrait.php:176
/var/www/sonata-doctrine-extensions/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:513
/var/www/sonata-doctrine-extensions/vendor/symfony/doctrine-bridge/CacheWarmer/ProxyCacheWarmer.php:48
/var/www/sonata-doctrine-extensions/vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php:108
/var/www/sonata-doctrine-extensions/vendor/symfony/http-kernel/Kernel.php:545
/var/www/sonata-doctrine-extensions/vendor/symfony/http-kernel/Kernel.php:763
/var/www/sonata-doctrine-extensions/vendor/symfony/http-kernel/Kernel.php:126
/var/www/sonata-doctrine-extensions/vendor/symfony/framework-bundle/Test/KernelTestCase.php:73
/var/www/sonata-doctrine-extensions/tests/Mapper/ORM/DoctrineORMMapperTest.php:58
/var/www/sonata-doctrine-extensions/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
/var/www/sonata-doctrine-extensions/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/var/www/sonata-doctrine-extensions/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/var/www/sonata-doctrine-extensions/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/var/www/sonata-doctrine-extensions/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651

'mappings' => [
'Entity' => [
'type' => 'attribute',
Expand Down
4 changes: 2 additions & 2 deletions tests/Mapper/DoctrineCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\Doctrine\Tests\Mapper;

use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use PHPUnit\Framework\TestCase;
use Sonata\Doctrine\Mapper\Builder\ColumnDefinitionBuilder;
use Sonata\Doctrine\Mapper\Builder\OptionsBuilder;
Expand Down Expand Up @@ -46,7 +46,7 @@ public function testClear(): void
$collector = DoctrineCollector::getInstance();
$collector->addIndex(\stdClass::class, 'name', ['column']);
$collector->addUnique(\stdClass::class, 'name', ['column']);
$collector->addInheritanceType(\stdClass::class, ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE);
$collector->addInheritanceType(\stdClass::class, ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
$collector->addDiscriminatorColumn(\stdClass::class, ColumnDefinitionBuilder::create()
->add('columnDef', ''));
$collector->addAssociation(\stdClass::class, 'type', OptionsBuilder::createOneToOne('foo', 'bar')
Expand Down
43 changes: 41 additions & 2 deletions tests/Mapper/ORM/DoctrineORMMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

namespace Sonata\Doctrine\Tests\Mapper\ORM;

use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\DiscriminatorColumnMapping;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Sonata\Doctrine\Mapper\Builder\ColumnDefinitionBuilder;
use Sonata\Doctrine\Mapper\Builder\OptionsBuilder;
use Sonata\Doctrine\Mapper\DoctrineCollector;
Expand Down Expand Up @@ -43,7 +46,7 @@ public static function setUpBeforeClass(): void

$collector->addDiscriminator(TestEntity::class, 'test', TestInheritanceEntity::class);
$collector->addDiscriminatorColumn(TestEntity::class, $columnDefinition);
$collector->addInheritanceType(TestEntity::class, ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE);
$collector->addInheritanceType(TestEntity::class, ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
$collector->addAssociation(TestEntity::class, 'mapManyToOne', $options);
$collector->addOverride(TestEntity::class, 'setAttributeOverride', $override);
}
Expand All @@ -62,6 +65,42 @@ public function testDoctrineMappingLoaded(): void
static::assertInstanceOf(DoctrineORMMapper::class, $mapper);
}

public function testLoadClassMetadata(): void
{
self::bootKernel();

/** @var DoctrineORMMapper $mapper */
$mapper = static::getContainer()->get('sonata.doctrine.mapper');

/** @var EntityManagerInterface $entityManager */
$entityManager = static::getContainer()->get('doctrine.orm.entity_manager');

$classMetadata = $entityManager->getClassMetadata(TestEntity::class);
$classMetadata->setDiscriminatorColumn([
'name' => 'discriminator',
'length' => 10,
]);

$mapper->loadClassMetadata(new LoadClassMetadataEventArgs($classMetadata, $entityManager));

$expectedDefinition = [
'type' => 'string',
'fieldName' => 'discriminator',
'name' => 'discriminator',
'length' => 10,
];

$expected = (array) ($classMetadata->discriminatorColumn instanceof DiscriminatorColumnMapping
? DiscriminatorColumnMapping::fromMappingArray($expectedDefinition)
: $expectedDefinition);
ksort($expected);

$actual = (array) $classMetadata->discriminatorColumn;
ksort($actual);

static::assertSame($expected, $actual);
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return class-string
*/
Expand Down
Loading