Skip to content

Commit 7c515e1

Browse files
committed
Add support for Doctrine ORM 3
1 parent eebdf34 commit 7c515e1

File tree

12 files changed

+57
-29
lines changed

12 files changed

+57
-29
lines changed

.github/workflows/build.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ on:
1313
jobs:
1414
tests:
1515
runs-on: ubuntu-latest
16-
name: "PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}"
16+
name: "PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}, ORM ${{ matrix.orm }}"
1717
env:
1818
APP_ENV: ${{ matrix.app_env }}
1919
strategy:
2020
fail-fast: false
2121
matrix:
22+
orm: ['2.*', '3.*']
2223
php: ["8.1", "8.2", "8.3"]
2324
composer-flags: ['--no-scripts --prefer-stable --prefer-dist']
2425
symfony: ["^6.4", "^7.1"]
@@ -47,6 +48,13 @@ jobs:
4748
composer config extra.symfony.require "${{ matrix.symfony }}"
4849
(cd src/Component && composer config extra.symfony.require "${{ matrix.symfony }}")
4950
51+
-
52+
name: Restrict ORM version
53+
if: matrix.orm != ''
54+
run: |
55+
composer require --dev doctrine/orm "${{ matrix.orm }}" --no-update --no-scripts
56+
(cd src/Component && composer require --dev doctrine/orm "${{ matrix.orm }}" --no-update --no-scripts)
57+
5058
-
5159
name: Remove hateoas on Symfony 7
5260
if: matrix.symfony == '^7.0'
@@ -55,13 +63,12 @@ jobs:
5563
-
5664
name: Install dependencies
5765
run: |
58-
composer update --no-scripts
66+
composer update ${{ matrix.composer-flags }}
5967
(cd src/Component && composer update ${{ matrix.composer-flags }})
6068
6169
-
6270
name: Prepare test application
6371
run: |
64-
(cd tests/Application && bin/console doctrine:database:create)
6572
(cd tests/Application && bin/console doctrine:schema:create)
6673
6774
-

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ RUN composer update --with-all-dependencies --no-interaction --no-progress
2121

2222
WORKDIR /app/tests/Application
2323

24-
RUN php bin/console doctrine:database:create && php bin/console doctrine:schema:update --force
24+
RUN php bin/console doctrine:schema:create
2525

2626
WORKDIR /app

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"require-dev": {
5858
"doctrine/doctrine-bundle": "^2.13",
59-
"doctrine/orm": "^2.18",
59+
"doctrine/orm": "^2.18 || ^3.3",
6060
"friendsofsymfony/rest-bundle": "^3.7",
6161
"jms/serializer-bundle": "^3.5 || ^4.0 || ^5.0",
6262
"lchrusciel/api-test-case": "^5.0",
@@ -89,7 +89,7 @@
8989
"winzou/state-machine-bundle": "^0.6.2"
9090
},
9191
"conflict": {
92-
"doctrine/orm": "<2.18 || ^3.0",
92+
"doctrine/orm": "<2.18",
9393
"doctrine/doctrine-bundle": "<2.0 || ^3.0",
9494
"friendsofsymfony/rest-bundle": "<3.0",
9595
"jms/serializer-bundle": "<3.5",

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ parameters:
4040
- %currentWorkingDirectory%/src/Component/vendor/*
4141

4242
ignoreErrors:
43+
- '/Call to method type\(\) on an unknown class Doctrine\\ORM\\Mapping\\AssociationMapping./'
4344
- '/Call to method getArguments\(\) on an unknown class ReflectionAttribute./'
4445
- '/Call to method isChangeTrackingDeferredExplicit\(\) on an unknown class Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata./'
4546
- '/Call to an undefined method ReflectionClass::getAttributes\(\)./'
@@ -75,6 +76,7 @@ parameters:
7576
- '/Parameter \#2 \$class of static method Webmozart\\Assert\\Assert::isInstanceOf\(\) expects class-string<object>, string given./'
7677
- '/Parameter \#1 \$objectOrClass of class ReflectionClass constructor expects class-string<object>\|object, object\|string given./'
7778
- '/Parameter \#1 \$package of method Symfony\\Component\\DependencyInjection\\Alias::setDeprecated\(\)/'
79+
- '/PHPDoc tag @var for variable \$value contains unknown class Doctrine\\ORM\\Mapping\\AssociationMapping./'
7880
- '/Return typehint of method Sylius\\Bundle\\ResourceBundle\\Routing\\CrudRoutesAttributesLoader::getClassAttributes\(\) has invalid type ReflectionAttribute./'
7981
- '/Return typehint of method Sylius\\Bundle\\ResourceBundle\\Routing\\RoutesAttributesLoader::getClassAttributes\(\) has invalid type ReflectionAttribute./'
8082
- '/Unable to resolve the template type ExpectedType in call to method static method Webmozart\\Assert\\Assert::isInstanceOf\(\)/'

psalm.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
</errorLevel>
9696
</InvalidArgument>
9797

98+
<InvalidPropertyAssignmentValue>
99+
<errorLevel type="suppress">
100+
<file name="src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php" />
101+
</errorLevel>
102+
</InvalidPropertyAssignmentValue>
103+
98104
<InvalidReturnStatement>
99105
<errorLevel type="suppress">
100106
<file name="src/Component/src/Doctrine/Persistence/InMemoryRepository.php" />
@@ -281,7 +287,8 @@
281287

282288
<UndefinedDocblockClass>
283289
<errorLevel type="suppress">
284-
<referencedClass name="UnitEnum"/>
290+
<referencedClass name="UnitEnum" />
291+
<referencedClass name="Doctrine\ORM\Mapping\AssociationMapping" />
285292
</errorLevel>
286293
</UndefinedDocblockClass>
287294

src/Bundle/Doctrine/ORM/ContainerRepositoryFactory.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
namespace Sylius\Bundle\ResourceBundle\Doctrine\ORM;
1515

1616
use Doctrine\ORM\EntityManagerInterface;
17+
use Doctrine\ORM\EntityRepository as DoctrineEntityRepository;
1718
use Doctrine\ORM\Mapping\ClassMetadata;
1819
use Doctrine\ORM\Repository\RepositoryFactory;
19-
use Doctrine\Persistence\ObjectRepository;
2020

2121
final class ContainerRepositoryFactory implements RepositoryFactory
2222
{
@@ -25,7 +25,7 @@ final class ContainerRepositoryFactory implements RepositoryFactory
2525
/** @var string[] */
2626
private array $genericEntities;
2727

28-
/** @var ObjectRepository[] */
28+
/** @var DoctrineEntityRepository[] */
2929
private array $managedRepositories = [];
3030

3131
/**
@@ -37,8 +37,11 @@ public function __construct(RepositoryFactory $doctrineFactory, array $genericEn
3737
$this->genericEntities = $genericEntities;
3838
}
3939

40-
/** @psalm-suppress InvalidReturnType */
41-
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
40+
/**
41+
* @psalm-suppress InvalidReturnStatement
42+
* @psalm-suppress InvalidReturnType
43+
*/
44+
public function getRepository(EntityManagerInterface $entityManager, $entityName): DoctrineEntityRepository
4245
{
4346
$metadata = $entityManager->getClassMetadata($entityName);
4447

@@ -47,13 +50,16 @@ public function getRepository(EntityManagerInterface $entityManager, $entityName
4750
return $this->getOrCreateRepository($entityManager, $metadata);
4851
}
4952

50-
return $this->doctrineFactory->getRepository($entityManager, $entityName);
53+
/** @var DoctrineEntityRepository $repository */
54+
$repository = $this->doctrineFactory->getRepository($entityManager, $entityName);
55+
56+
return $repository;
5157
}
5258

5359
private function getOrCreateRepository(
5460
EntityManagerInterface $entityManager,
5561
ClassMetadata $metadata,
56-
): ObjectRepository {
62+
): DoctrineEntityRepository {
5763
$repositoryHash = $metadata->getName() . spl_object_hash($entityManager);
5864

5965
if (!isset($this->managedRepositories[$repositoryHash])) {

src/Bundle/Doctrine/ORM/ResourceRepositoryTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ trait ResourceRepositoryTrait
3232
{
3333
public function add(ResourceInterface $resource): void
3434
{
35-
$this->_em->persist($resource);
36-
$this->_em->flush();
35+
$this->getEntityManager()->persist($resource);
36+
$this->getEntityManager()->flush();
3737
}
3838

3939
public function remove(ResourceInterface $resource): void
4040
{
4141
if (null !== $this->find($resource->getId())) {
42-
$this->_em->remove($resource);
43-
$this->_em->flush();
42+
$this->getEntityManager()->remove($resource);
43+
$this->getEntityManager()->flush();
4444
}
4545
}
4646

@@ -78,7 +78,7 @@ protected function getArrayPaginator($objects): Pagerfanta
7878
protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = []): void
7979
{
8080
foreach ($criteria as $property => $value) {
81-
if (!in_array($property, array_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
81+
if (!in_array($property, array_merge($this->getClassMetadata()->getAssociationNames(), $this->getClassMetadata()->getFieldNames()), true)) {
8282
continue;
8383
}
8484

@@ -101,7 +101,7 @@ protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = [
101101
protected function applySorting(QueryBuilder $queryBuilder, array $sorting = []): void
102102
{
103103
foreach ($sorting as $property => $order) {
104-
if (!in_array($property, array_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
104+
if (!in_array($property, array_merge($this->getClassMetadata()->getAssociationNames(), $this->getClassMetadata()->getFieldNames()), true)) {
105105
continue;
106106
}
107107

src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\ORM\Configuration;
1818
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
1919
use Doctrine\ORM\Events;
20+
use Doctrine\ORM\Mapping\AssociationMapping;
2021
use Doctrine\ORM\Mapping\ClassMetadata;
2122
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
2223
use Webmozart\Assert\Assert;
@@ -80,9 +81,13 @@ private function setAssociationMappings(ClassMetadata $metadata, Configuration $
8081
}
8182

8283
if ($parentMetadata->isMappedSuperclass) {
84+
/**
85+
* @var AssociationMapping|array{type: int} $value
86+
*/
8387
foreach ($parentMetadata->getAssociationMappings() as $key => $value) {
84-
if ($this->isRelation($value['type']) && !isset($metadata->associationMappings[$key])) {
85-
$metadata->associationMappings[$key] = $value;
88+
$type = \is_array($value) ? $value['type'] : $value->type();
89+
if ($this->isRelation($type) && !isset($metadata->associationMappings[$key])) {
90+
$metadata->associationMappings[$key] = $value; /** @phpstan-ignore-line */
8691
}
8792
}
8893
}
@@ -96,8 +101,12 @@ private function unsetAssociationMappings(ClassMetadata $metadata): void
96101
return;
97102
}
98103

104+
/**
105+
* @var AssociationMapping|array{type: int} $value
106+
*/
99107
foreach ($metadata->getAssociationMappings() as $key => $value) {
100-
if ($this->isRelation($value['type'])) {
108+
$type = \is_array($value) ? $value['type'] : $value->type();
109+
if ($this->isRelation($type)) {
101110
unset($metadata->associationMappings[$key]);
102111
}
103112
}

src/Bundle/EventListener/ORMTranslatableListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
5858
$classMetadata = $eventArgs->getClassMetadata();
5959
$reflection = $classMetadata->getReflectionClass();
6060

61+
/** @psalm-suppress PossiblyNullReference */
6162
if ($reflection->isAbstract()) {
6263
return;
6364
}
@@ -109,7 +110,7 @@ private function mapTranslatable(ClassMetadata $metadata): void
109110
'mappedBy' => 'translatable',
110111
'fetch' => ClassMetadata::FETCH_EXTRA_LAZY,
111112
'indexBy' => 'locale',
112-
'cascade' => ['persist', 'merge', 'remove'],
113+
'cascade' => ['persist', 'remove'],
113114
'orphanRemoval' => true,
114115
]);
115116
}

src/Component/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"require-dev": {
5050
"behat/transliterator": "^1.3",
51-
"doctrine/orm": "^2.18",
51+
"doctrine/orm": "^2.18 || ^3.3",
5252
"matthiasnoback/symfony-dependency-injection-test": "^4.2.1 || ^5.1",
5353
"phpspec/phpspec": "^7.3",
5454
"phpspec/prophecy-phpunit": "^2.0",
@@ -59,7 +59,7 @@
5959
"twig/twig": "^3.0"
6060
},
6161
"conflict": {
62-
"doctrine/orm": "<2.18 || ^3.0",
62+
"doctrine/orm": "<2.18",
6363
"twig/twig": "<3.0"
6464
},
6565
"extra": {

0 commit comments

Comments
 (0)