Skip to content

Commit

Permalink
Add support for Doctrine ORM 3
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jan 14, 2025
1 parent eebdf34 commit 54666ac
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 22 deletions.
17 changes: 14 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ on:
jobs:
tests:
runs-on: ubuntu-latest
name: "PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}"
name: "PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}, ORM ${{ matrix.orm }}"
env:
APP_ENV: ${{ matrix.app_env }}
strategy:
fail-fast: false
matrix:
orm: ['2.*', '3.*']
php: ["8.1", "8.2", "8.3"]
composer-flags: ['--no-scripts --prefer-stable --prefer-dist']
symfony: ["^6.4", "^7.1"]
app_env: ["test"]
include:
- php: "8.3"
symfony: "^7.1"
composer-flags: '--no-scripts --prefer-stable --prefer-dist --prefer-lowest'
exclude:
- php: "8.1"
symfony: "^7.1"
Expand All @@ -47,6 +52,13 @@ jobs:
composer config extra.symfony.require "${{ matrix.symfony }}"
(cd src/Component && composer config extra.symfony.require "${{ matrix.symfony }}")
-
name: Restrict ORM version
if: matrix.orm != ''
run: |
composer require --dev doctrine/orm "${{ matrix.orm }}" --no-update --no-scripts
(cd src/Component && composer require --dev doctrine/orm "${{ matrix.orm }}" --no-update --no-scripts)
-
name: Remove hateoas on Symfony 7
if: matrix.symfony == '^7.0'
Expand All @@ -55,13 +67,12 @@ jobs:
-
name: Install dependencies
run: |
composer update --no-scripts
composer update ${{ matrix.composer-flags }}
(cd src/Component && composer update ${{ matrix.composer-flags }})
-
name: Prepare test application
run: |
(cd tests/Application && bin/console doctrine:database:create)
(cd tests/Application && bin/console doctrine:schema:create)
-
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.13",
"doctrine/orm": "^2.18",
"doctrine/orm": "^2.18 || ^3.3",
"friendsofsymfony/rest-bundle": "^3.7",
"jms/serializer-bundle": "^3.5 || ^4.0 || ^5.0",
"lchrusciel/api-test-case": "^5.0",
Expand Down Expand Up @@ -89,7 +89,7 @@
"winzou/state-machine-bundle": "^0.6.2"
},
"conflict": {
"doctrine/orm": "<2.18 || ^3.0",
"doctrine/orm": "<2.18",
"doctrine/doctrine-bundle": "<2.0 || ^3.0",
"friendsofsymfony/rest-bundle": "<3.0",
"jms/serializer-bundle": "<3.5",
Expand Down
8 changes: 4 additions & 4 deletions src/Bundle/Doctrine/ORM/ContainerRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
namespace Sylius\Bundle\ResourceBundle\Doctrine\ORM;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository as DoctrineEntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\Persistence\ObjectRepository;

final class ContainerRepositoryFactory implements RepositoryFactory
{
Expand All @@ -25,7 +25,7 @@ final class ContainerRepositoryFactory implements RepositoryFactory
/** @var string[] */
private array $genericEntities;

/** @var ObjectRepository[] */
/** @var DoctrineEntityRepository[] */
private array $managedRepositories = [];

/**
Expand All @@ -38,7 +38,7 @@ public function __construct(RepositoryFactory $doctrineFactory, array $genericEn
}

/** @psalm-suppress InvalidReturnType */
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
public function getRepository(EntityManagerInterface $entityManager, $entityName): DoctrineEntityRepository
{
$metadata = $entityManager->getClassMetadata($entityName);

Expand All @@ -53,7 +53,7 @@ public function getRepository(EntityManagerInterface $entityManager, $entityName
private function getOrCreateRepository(
EntityManagerInterface $entityManager,
ClassMetadata $metadata,
): ObjectRepository {
): DoctrineEntityRepository {
$repositoryHash = $metadata->getName() . spl_object_hash($entityManager);

if (!isset($this->managedRepositories[$repositoryHash])) {
Expand Down
12 changes: 6 additions & 6 deletions src/Bundle/Doctrine/ORM/ResourceRepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ trait ResourceRepositoryTrait
{
public function add(ResourceInterface $resource): void
{
$this->_em->persist($resource);
$this->_em->flush();
$this->getEntityManager()->persist($resource);
$this->getEntityManager()->flush();
}

public function remove(ResourceInterface $resource): void
{
if (null !== $this->find($resource->getId())) {
$this->_em->remove($resource);
$this->_em->flush();
$this->getEntityManager()->remove($resource);
$this->getEntityManager()->flush();
}
}

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

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

Expand Down
1 change: 1 addition & 0 deletions src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private function setAssociationMappings(ClassMetadata $metadata, Configuration $
if ($parentMetadata->isMappedSuperclass) {
foreach ($parentMetadata->getAssociationMappings() as $key => $value) {
if ($this->isRelation($value['type']) && !isset($metadata->associationMappings[$key])) {

Check failure on line 84 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

Parameter #1 $type of method Sylius\Bundle\ResourceBundle\EventListener\ORMMappedSuperClassSubscriber::isRelation() expects int, mixed given.

Check failure on line 84 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

Parameter #1 $type of method Sylius\Bundle\ResourceBundle\EventListener\ORMMappedSuperClassSubscriber::isRelation() expects int, mixed given.

Check failure on line 84 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

Parameter #1 $type of method Sylius\Bundle\ResourceBundle\EventListener\ORMMappedSuperClassSubscriber::isRelation() expects int, mixed given.

Check failure on line 84 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

Parameter #1 $type of method Sylius\Bundle\ResourceBundle\EventListener\ORMMappedSuperClassSubscriber::isRelation() expects int, mixed given.

Check failure on line 84 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

Parameter #1 $type of method Sylius\Bundle\ResourceBundle\EventListener\ORMMappedSuperClassSubscriber::isRelation() expects int, mixed given.
/** @psalm-suppress PropertyTypeCoercion */
$metadata->associationMappings[$key] = $value;

Check failure on line 86 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

Property Doctrine\ORM\Mapping\ClassMetadata<object>::$associationMappings (array<string, Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping>) does not accept array<string, Doctrine\ORM\Mapping\AssociationMapping>.

Check failure on line 86 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

Property Doctrine\ORM\Mapping\ClassMetadata<object>::$associationMappings (array<string, Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping>) does not accept array<string, Doctrine\ORM\Mapping\AssociationMapping>.

Check failure on line 86 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

Property Doctrine\ORM\Mapping\ClassMetadata<object>::$associationMappings (array<string, Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping>) does not accept array<string, Doctrine\ORM\Mapping\AssociationMapping>.

Check failure on line 86 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

Property Doctrine\ORM\Mapping\ClassMetadata<object>::$associationMappings (array<string, Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping>) does not accept array<string, Doctrine\ORM\Mapping\AssociationMapping>.

Check failure on line 86 in src/Bundle/EventListener/ORMMappedSuperClassSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

Property Doctrine\ORM\Mapping\ClassMetadata<object>::$associationMappings (array<string, Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping>) does not accept array<string, Doctrine\ORM\Mapping\AssociationMapping>.
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Bundle/EventListener/ORMTranslatableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
$classMetadata = $eventArgs->getClassMetadata();
$reflection = $classMetadata->getReflectionClass();

if (null === $reflection) {

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 2.*

TypeDoesNotContainNull

src/Bundle/EventListener/ORMTranslatableListener.php:61:13: TypeDoesNotContainNull: ReflectionClass<T> does not contain null (see https://psalm.dev/090)

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 2.*

TypeDoesNotContainNull

src/Bundle/EventListener/ORMTranslatableListener.php:61:13: TypeDoesNotContainNull: ReflectionClass<T> does not contain null (see https://psalm.dev/090)

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 2.*

TypeDoesNotContainNull

src/Bundle/EventListener/ORMTranslatableListener.php:61:13: TypeDoesNotContainNull: ReflectionClass<T> does not contain null (see https://psalm.dev/090)

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 2.*

TypeDoesNotContainNull

src/Bundle/EventListener/ORMTranslatableListener.php:61:13: TypeDoesNotContainNull: ReflectionClass<T> does not contain null (see https://psalm.dev/090)

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

Strict comparison using === between null and ReflectionClass<object> will always evaluate to false.

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 2.*

TypeDoesNotContainNull

src/Bundle/EventListener/ORMTranslatableListener.php:61:13: TypeDoesNotContainNull: ReflectionClass<T> does not contain null (see https://psalm.dev/090)

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

Strict comparison using === between null and ReflectionClass<object> will always evaluate to false.

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

Strict comparison using === between null and ReflectionClass<object> will always evaluate to false.

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

Strict comparison using === between null and ReflectionClass<object> will always evaluate to false.

Check failure on line 61 in src/Bundle/EventListener/ORMTranslatableListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

Strict comparison using === between null and ReflectionClass<object> will always evaluate to false.
return;
}

if ($reflection->isAbstract()) {
return;
}
Expand Down Expand Up @@ -109,7 +113,7 @@ private function mapTranslatable(ClassMetadata $metadata): void
'mappedBy' => 'translatable',
'fetch' => ClassMetadata::FETCH_EXTRA_LAZY,
'indexBy' => 'locale',
'cascade' => ['persist', 'merge', 'remove'],
'cascade' => ['persist', 'remove'],
'orphanRemoval' => true,
]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Component/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"require-dev": {
"behat/transliterator": "^1.3",
"doctrine/orm": "^2.18",
"doctrine/orm": "^2.18 || ^3.3",
"matthiasnoback/symfony-dependency-injection-test": "^4.2.1 || ^5.1",
"phpspec/phpspec": "^7.3",
"phpspec/prophecy-phpunit": "^2.0",
Expand All @@ -59,7 +59,7 @@
"twig/twig": "^3.0"
},
"conflict": {
"doctrine/orm": "<2.18 || ^3.0",
"doctrine/orm": "<2.18",
"twig/twig": "<3.0"
},
"extra": {
Expand Down
2 changes: 0 additions & 2 deletions tests/Application/src/Entity/GedmoBaseExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
use Gedmo\Mapping\Annotation as Gedmo;
use Sylius\Resource\Model\ResourceInterface;

#[ORM\Entity]
#[ORM\MappedSuperclass]
#[ORM\Table(name: 'gedmo')]
class GedmoBaseExample implements ResourceInterface
{
#[ORM\Id]
Expand Down
2 changes: 0 additions & 2 deletions tests/Application/src/Entity/GedmoExtendedExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\MappedSuperclass]

class GedmoExtendedExample extends GedmoBaseExample
{
#[ORM\Column(length: 255)]
Expand Down

0 comments on commit 54666ac

Please sign in to comment.