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

chore: upgrade to doctrine/orm 3 #5365

Draft
wants to merge 4 commits into
base: devel
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"cweagans/composer-patches": "1.7.3",
"doctrine/doctrine-bundle": "2.12.0",
"doctrine/doctrine-migrations-bundle": "3.3.1",
"doctrine/orm": "2.19.6",
"doctrine/orm": "3.2.0",
"exercise/htmlpurifier-bundle": "5.0",
"friendsofsymfony/http-cache": "3.0.0",
"friendsofsymfony/http-cache-bundle": "3.0.1",
Expand Down Expand Up @@ -52,7 +52,7 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.59.3",
"hautelook/alice-bundle": "2.13.0",
"hautelook/alice-bundle": "2.14.0",
"justinrainbow/json-schema": "5.3.0",
"php-coveralls/php-coveralls": "2.7.0",
"phpspec/prophecy-phpunit": "2.2",
Expand Down
84 changes: 36 additions & 48 deletions api/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions api/src/HttpCache/PurgeHttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\PersistentCollection;
use FOS\HttpCacheBundle\CacheManager;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand All @@ -51,7 +51,7 @@ public function __construct(private readonly IriConverterInterface|LegacyIriConv
*/
public function preUpdate(PreUpdateEventArgs $eventArgs): void {
$changeSet = $eventArgs->getEntityChangeSet();
$objectManager = method_exists($eventArgs, 'getObjectManager') ? $eventArgs->getObjectManager() : $eventArgs->getEntityManager();
$objectManager = $eventArgs->getObjectManager();
$associationMappings = $objectManager->getClassMetadata(ClassUtils::getClass($eventArgs->getObject()))->getAssociationMappings();

foreach ($changeSet as $key => $value) {
Expand All @@ -74,7 +74,12 @@ public function preUpdate(PreUpdateEventArgs $eventArgs): void {
*/
public function onFlush(OnFlushEventArgs $eventArgs): void {
/** @var EntityManagerInterface */
$em = method_exists($eventArgs, 'getObjectManager') ? $eventArgs->getObjectManager() : $eventArgs->getEntityManager();
$em = $eventArgs->getObjectManager();

if (!$em instanceof EntityManagerInterface) {
return;
}

$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityInsertions() as $entity) {
Expand Down Expand Up @@ -116,7 +121,7 @@ public function postFlush(): void {
private function addTagsForManyToManyRelations($collection, $entities) {
$associationMapping = $collection->getMapping();

if (ClassMetadataInfo::MANY_TO_MANY !== $associationMapping['type']) {
if (ClassMetadata::MANY_TO_MANY !== $associationMapping['type']) {
return;
}

Expand Down Expand Up @@ -220,15 +225,7 @@ private function gatherRelationTags(EntityManagerInterface $em, object $entity):
$associationMappings = $em->getClassMetadata(ClassUtils::getClass($entity))->getAssociationMappings();

foreach ($associationMappings as $property => $associationMapping) {
// @phpstan-ignore-next-line
if (class_exists(AssociationMapping::class) && $associationMapping instanceof AssociationMapping && ($associationMapping->targetEntity ?? null) && !$this->resourceClassResolver->isResourceClass($associationMapping->targetEntity)) {
return;
}

// @phpstan-ignore-next-line
if (\is_array($associationMapping)
&& \array_key_exists('targetEntity', $associationMapping)
&& !$this->resourceClassResolver->isResourceClass($associationMapping['targetEntity'])) {
if ($associationMapping instanceof AssociationMapping && ($associationMapping->targetEntity ?? null) && !$this->resourceClassResolver->isResourceClass($associationMapping->targetEntity)) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions api/src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public function upgradePassword(PasswordAuthenticatedUserInterface $user, string
}

$user->password = $newHashedPassword;
$this->_em->persist($user);
$this->_em->flush();
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}

/**
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function loadUserByIdentifier(string $identifier): ?User {
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$queryBuilder->select('user');
$queryBuilder->from(User::class, 'user');
$queryBuilder->join('user.profile', 'profile');
Expand Down
28 changes: 20 additions & 8 deletions api/src/Serializer/Normalizer/RelatedCollectionLinkNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use App\Metadata\Resource\OperationHelper;
use App\Util\ClassInfoTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\InverseSideMapping;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\OwningSideMapping;
use Rize\UriTemplate;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand Down Expand Up @@ -155,19 +157,17 @@ public function getRelatedCollectionHref($object, $rel, array $context = []): st
try {
$classMetadata = $this->getClassMetadata($resourceClass);

if (!$classMetadata instanceof ClassMetadataInfo) {
throw new \RuntimeException("The class metadata for {$resourceClass} must be an instance of ClassMetadataInfo.");
if (!$classMetadata instanceof ClassMetadata) {
throw new \RuntimeException("The class metadata for {$resourceClass} must be an instance of ClassMetadata.");
}

$relationMetadata = $classMetadata->getAssociationMapping($rel);
} catch (MappingException) {
throw new UnsupportedRelationException($resourceClass.'#'.$rel.' is not a Doctrine association. Embedding non-Doctrine collections is currently not implemented.');
}

$relatedResourceClass = $relationMetadata['targetEntity'];

$relatedFilterName = $relationMetadata['mappedBy'];
$relatedFilterName ??= $relationMetadata['inversedBy'];
$relatedResourceClass = $relationMetadata->targetEntity;
$relatedFilterName = $this->getRelatedProperty($relationMetadata);

if (empty($relatedResourceClass) || empty($relatedFilterName)) {
throw new UnsupportedRelationException('The '.$resourceClass.'#'.$rel.' relation does not have both a targetEntity and a mappedBy or inversedBy property');
Expand Down Expand Up @@ -251,4 +251,16 @@ private function exactSearchFilterExists(string $resourceClass, mixed $propertyN
&& 'exact' === $filterDescription[$propertyName]['strategy'];
}));
}

private function getRelatedProperty(AssociationMapping $mapping): ?string {
if ($mapping instanceof InverseSideMapping) {
return $mapping->mappedBy ?? null;
}

if ($mapping instanceof OwningSideMapping) {
return $mapping->inversedBy ?? null;
}

return null;
}
}
4 changes: 2 additions & 2 deletions api/src/Util/IdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace App\Util;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Id\AbstractIdGenerator;

class IdGenerator extends AbstractIdGenerator {
public static function generateRandomHexString(int $length): string {
return bin2hex(random_bytes($length / 2));
}

public function generate(EntityManager $em, $entity): string {
public function generateId(EntityManagerInterface $em, ?object $entity): mixed {
return IdGenerator::generateRandomHexString(12);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Tests\Metadata\Resource\Factory;

use ApiPlatform\Api\FilterInterface;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\FilterInterface;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\IriConverterInterface;
Expand Down
Loading
Loading