Skip to content

Commit 816e9b2

Browse files
authored
Merge pull request ecamp#5365 from usu/chore/doctrine-orm-3
chore: upgrade to doctrine/orm 3
2 parents 3a2e474 + c10d591 commit 816e9b2

File tree

13 files changed

+193
-167
lines changed

13 files changed

+193
-167
lines changed

.github/workflows/continuous-integration-optional.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,5 @@ jobs:
136136
- run: php bin/console doctrine:migrations:migrate --no-interaction -e test
137137
working-directory: api
138138

139-
- run: php bin/console doctrine:schema:validate -v -e test
139+
- run: php bin/console doctrine:schema:validate --skip-sync -e test && php bin/console doctrine:migrations:up-to-date -e test && ! php bin/console doctrine:migrations:diff -e test --namespace DoctrineMigrations
140140
working-directory: api

api/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"cweagans/composer-patches": "1.7.3",
1111
"doctrine/doctrine-bundle": "2.13.1",
1212
"doctrine/doctrine-migrations-bundle": "3.3.1",
13-
"doctrine/orm": "2.20.0",
13+
"doctrine/orm": "3.2.0",
1414
"exercise/htmlpurifier-bundle": "5.0",
1515
"friendsofsymfony/http-cache": "3.1.0",
1616
"friendsofsymfony/http-cache-bundle": "3.0.2",
@@ -53,7 +53,7 @@
5353
"require-dev": {
5454
"brianium/paratest": "v7.7.0",
5555
"friendsofphp/php-cs-fixer": "3.66.0",
56-
"hautelook/alice-bundle": "2.13.0",
56+
"hautelook/alice-bundle": "2.14.0",
5757
"justinrainbow/json-schema": "6.0.0",
5858
"php-coveralls/php-coveralls": "2.7.0",
5959
"phpspec/prophecy-phpunit": "2.3.0",

api/composer.lock

Lines changed: 49 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/HttpCache/PurgeHttpCacheListener.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
use Doctrine\ORM\Event\OnFlushEventArgs;
3434
use Doctrine\ORM\Event\PreUpdateEventArgs;
3535
use Doctrine\ORM\Mapping\AssociationMapping;
36-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
36+
use Doctrine\ORM\Mapping\ClassMetadata;
3737
use Doctrine\ORM\PersistentCollection;
3838
use FOS\HttpCacheBundle\CacheManager;
3939
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -53,7 +53,7 @@ public function __construct(private readonly IriConverterInterface|LegacyIriConv
5353
*/
5454
public function preUpdate(PreUpdateEventArgs $eventArgs): void {
5555
$changeSet = $eventArgs->getEntityChangeSet();
56-
$objectManager = method_exists($eventArgs, 'getObjectManager') ? $eventArgs->getObjectManager() : $eventArgs->getEntityManager();
56+
$objectManager = $eventArgs->getObjectManager();
5757
$associationMappings = $objectManager->getClassMetadata(ClassUtils::getClass($eventArgs->getObject()))->getAssociationMappings();
5858

5959
foreach ($changeSet as $key => $value) {
@@ -76,7 +76,12 @@ public function preUpdate(PreUpdateEventArgs $eventArgs): void {
7676
*/
7777
public function onFlush(OnFlushEventArgs $eventArgs): void {
7878
/** @var EntityManagerInterface */
79-
$em = method_exists($eventArgs, 'getObjectManager') ? $eventArgs->getObjectManager() : $eventArgs->getEntityManager();
79+
$em = $eventArgs->getObjectManager();
80+
81+
if (!$em instanceof EntityManagerInterface) {
82+
return;
83+
}
84+
8085
$uow = $em->getUnitOfWork();
8186

8287
foreach ($uow->getScheduledEntityInsertions() as $entity) {
@@ -118,7 +123,7 @@ public function postFlush(): void {
118123
private function addTagsForManyToManyRelations($collection, $entities) {
119124
$associationMapping = $collection->getMapping();
120125

121-
if (ClassMetadataInfo::MANY_TO_MANY !== $associationMapping['type']) {
126+
if (ClassMetadata::MANY_TO_MANY !== $associationMapping['type']) {
122127
return;
123128
}
124129

@@ -222,15 +227,7 @@ private function gatherRelationTags(EntityManagerInterface $em, object $entity):
222227
$associationMappings = $em->getClassMetadata(ClassUtils::getClass($entity))->getAssociationMappings();
223228

224229
foreach ($associationMappings as $property => $associationMapping) {
225-
// @phpstan-ignore-next-line
226-
if (class_exists(AssociationMapping::class) && $associationMapping instanceof AssociationMapping && ($associationMapping->targetEntity ?? null) && !$this->resourceClassResolver->isResourceClass($associationMapping->targetEntity)) {
227-
return;
228-
}
229-
230-
// @phpstan-ignore-next-line
231-
if (\is_array($associationMapping)
232-
&& \array_key_exists('targetEntity', $associationMapping)
233-
&& !$this->resourceClassResolver->isResourceClass($associationMapping['targetEntity'])) {
230+
if ($associationMapping instanceof AssociationMapping && ($associationMapping->targetEntity ?? null) && !$this->resourceClassResolver->isResourceClass($associationMapping->targetEntity)) {
234231
return;
235232
}
236233

api/src/Repository/UserRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ public function upgradePassword(PasswordAuthenticatedUserInterface $user, string
3232
}
3333

3434
$user->password = $newHashedPassword;
35-
$this->_em->persist($user);
36-
$this->_em->flush();
35+
$this->getEntityManager()->persist($user);
36+
$this->getEntityManager()->flush();
3737
}
3838

3939
/**
4040
* @throws NonUniqueResultException
4141
* @throws NoResultException
4242
*/
4343
public function loadUserByIdentifier(string $identifier): ?User {
44-
$queryBuilder = $this->_em->createQueryBuilder();
44+
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
4545
$queryBuilder->select('user');
4646
$queryBuilder->from(User::class, 'user');
4747
$queryBuilder->join('user.profile', 'profile');

api/src/Serializer/Normalizer/RelatedCollectionLinkNormalizer.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
use App\Metadata\Resource\OperationHelper;
1717
use App\Util\ClassInfoTrait;
1818
use Doctrine\ORM\EntityManagerInterface;
19-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19+
use Doctrine\ORM\Mapping\AssociationMapping;
20+
use Doctrine\ORM\Mapping\ClassMetadata;
21+
use Doctrine\ORM\Mapping\InverseSideMapping;
2022
use Doctrine\ORM\Mapping\MappingException;
21-
use Doctrine\Persistence\Mapping\ClassMetadata;
23+
use Doctrine\ORM\Mapping\OwningSideMapping;
2224
use Rize\UriTemplate;
2325
use Symfony\Component\DependencyInjection\ServiceLocator;
2426
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -168,8 +170,8 @@ protected function getRelatedCollectionHref($object, $rel, array $context, &$hre
168170
try {
169171
$classMetadata = $this->getClassMetadata($resourceClass);
170172

171-
if (!$classMetadata instanceof ClassMetadataInfo) {
172-
throw new \RuntimeException("The class metadata for {$resourceClass} must be an instance of ClassMetadataInfo.");
173+
if (!$classMetadata instanceof ClassMetadata) {
174+
throw new \RuntimeException("The class metadata for {$resourceClass} must be an instance of ClassMetadata.");
173175
}
174176

175177
$relationMetadata = $classMetadata->getAssociationMapping($rel);
@@ -178,10 +180,8 @@ protected function getRelatedCollectionHref($object, $rel, array $context, &$hre
178180
return false;
179181
}
180182

181-
$relatedResourceClass = $relationMetadata['targetEntity'];
182-
183-
$relatedFilterName = $relationMetadata['mappedBy'];
184-
$relatedFilterName ??= $relationMetadata['inversedBy'];
183+
$relatedResourceClass = $relationMetadata->targetEntity;
184+
$relatedFilterName = $this->getRelatedProperty($relationMetadata);
185185

186186
if (empty($relatedResourceClass) || empty($relatedFilterName)) {
187187
// The $resourceClass # $rel relation does not have both a targetEntity and a mappedBy or inversedBy property
@@ -282,4 +282,16 @@ private function exactSearchFilterExists(string $resourceClass, mixed $propertyN
282282
&& 'exact' === $filterDescription[$propertyName]['strategy'];
283283
}));
284284
}
285+
286+
private function getRelatedProperty(AssociationMapping $mapping): ?string {
287+
if ($mapping instanceof InverseSideMapping) {
288+
return $mapping->mappedBy ?? null;
289+
}
290+
291+
if ($mapping instanceof OwningSideMapping) {
292+
return $mapping->inversedBy ?? null;
293+
}
294+
295+
return null;
296+
}
285297
}

0 commit comments

Comments
 (0)