Skip to content

Commit

Permalink
Port d05e8e8
Browse files Browse the repository at this point in the history
The original commit has been made backward-compatible, and some
properties of the entity manager have been marked as readonly.
Closes #5933
  • Loading branch information
greg0ire committed May 25, 2024
1 parent 37946d3 commit c963b1a
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 115 deletions.
21 changes: 21 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# Upgrade to 3.3

## Deprecated closed status for EntityManager

It is always open now. As a consequence, the following methods, classes and
properties have been deprecated and will be removed in 4.0:

- `EntityManagerInterface::isOpen()`. All implementations should return `true`.
- `EntityManagerInterface::close()`. All implementations and callers should call `clear()` instead.
- `AbstractEntityPersister::$uow`: get the unit of work from `AbstractEntityPersister::$em` instead
- `AbstractCollectionPersister::$uow`: get the unit of work from `AbstractCollectionPersister::$em` instead

Mutating the following properties is deprecated and will not be possible in 4.0:

- `EntityManager::$config`
- `EntityManager::$conn`
- `EntityManager::$eventManager`
- `EntityManager::$metadataFactory`
- `EntityManager::$proxyFactory`
- `EntityManager::$repositoryFactory`

# Upgrade to 3.2

## Deprecate the `NotSupported` exception
Expand Down
8 changes: 8 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
<ArgumentTypeCoercion>
<code><![CDATA[$cacheEntry]]></code>
</ArgumentTypeCoercion>
<DeprecatedProperty>
<code><![CDATA[$this->uow]]></code>
</DeprecatedProperty>
<NoInterfaceProperties>
<code><![CDATA[$cacheEntry->class]]></code>
</NoInterfaceProperties>
Expand Down Expand Up @@ -648,6 +651,11 @@
<code><![CDATA[[$this->unwrap(), 'add']]]></code>
</UndefinedMethod>
</file>
<file src="src/Persisters/Collection/AbstractCollectionPersister.php">
<DeprecatedProperty>
<code><![CDATA[$this->uow]]></code>
</DeprecatedProperty>
</file>
<file src="src/Persisters/Collection/ManyToManyPersister.php">
<PossiblyNullArgument>
<code><![CDATA[$collection->getOwner()]]></code>
Expand Down
36 changes: 18 additions & 18 deletions src/Cache/DefaultQueryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\UnitOfWork;

use function array_map;
use function array_shift;
Expand All @@ -32,7 +31,6 @@
*/
class DefaultQueryCache implements QueryCache
{
private readonly UnitOfWork $uow;
private readonly QueryCacheValidator $validator;
protected CacheLogger|null $cacheLogger = null;

Expand All @@ -45,7 +43,6 @@ public function __construct(
) {
$cacheConfig = $em->getConfiguration()->getSecondLevelCacheConfiguration();

$this->uow = $em->getUnitOfWork();
$this->cacheLogger = $cacheConfig->getCacheLogger();
$this->validator = $cacheConfig->getQueryValidator();
}
Expand Down Expand Up @@ -74,7 +71,8 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
$result = [];
$entityName = reset($rsm->aliasMap);
$hasRelation = ! empty($rsm->relationMap);
$persister = $this->uow->getEntityPersister($entityName);
$uow = $this->em->getUnitOfWork();
$persister = $uow->getEntityPersister($entityName);
assert($persister instanceof CachedEntityPersister);

$region = $persister->getCacheRegion();
Expand All @@ -100,15 +98,15 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
$this->cacheLogger?->entityCacheHit($regionName, $cacheKeys->identifiers[$index]);

if (! $hasRelation) {
$result[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints);
$result[$index] = $uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints);

continue;
}

$data = $entityEntry->data;

foreach ($entry['associations'] as $name => $assoc) {
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocPersister = $uow->getEntityPersister($assoc['targetEntity']);
assert($assocPersister instanceof CachedEntityPersister);

$assocRegion = $assocPersister->getCacheRegion();
Expand All @@ -121,12 +119,12 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
if ($assocEntry === null) {
$this->cacheLogger?->entityCacheMiss($assocRegion->getName(), $assocKey);

$this->uow->hydrationComplete();
$uow->hydrationComplete();

return null;
}

$data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);
$data[$name] = $uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);

$this->cacheLogger?->entityCacheHit($assocRegion->getName(), $assocKey);

Expand All @@ -149,12 +147,12 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
if ($assocEntry === null) {
$this->cacheLogger?->entityCacheMiss($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]);

$this->uow->hydrationComplete();
$uow->hydrationComplete();

return null;
}

$element = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);
$element = $uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);

$collection->hydrateSet($assocIndex, $element);

Expand Down Expand Up @@ -185,10 +183,10 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
}
}

$result[$index] = $this->uow->createEntity($entityEntry->class, $data, self::$hints);
$result[$index] = $uow->createEntity($entityEntry->class, $data, self::$hints);
}

$this->uow->hydrationComplete();
$uow->hydrationComplete();

return $result;
}
Expand Down Expand Up @@ -217,7 +215,8 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
$data = [];
$entityName = reset($rsm->aliasMap);
$rootAlias = key($rsm->aliasMap);
$persister = $this->uow->getEntityPersister($entityName);
$uow = $this->em->getUnitOfWork();
$persister = $uow->getEntityPersister($entityName);

if (! $persister instanceof CachedEntityPersister) {
throw NonCacheableEntity::fromEntity($entityName);
Expand All @@ -229,7 +228,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
assert($cm instanceof ClassMetadata);

foreach ($result as $index => $entity) {
$identifier = $this->uow->getEntityIdentifier($entity);
$identifier = $uow->getEntityIdentifier($entity);
$entityKey = new EntityCacheKey($cm->rootEntityName, $identifier);

if (($key->cacheMode & Cache::MODE_REFRESH) || ! $region->contains($entityKey)) {
Expand Down Expand Up @@ -296,16 +295,17 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
*/
private function storeAssociationCache(QueryCacheKey $key, AssociationMapping $assoc, mixed $assocValue): array|null
{
$assocPersister = $this->uow->getEntityPersister($assoc->targetEntity);
$uow = $this->em->getUnitOfWork();
$assocPersister = $uow->getEntityPersister($assoc->targetEntity);
$assocMetadata = $assocPersister->getClassMetadata();
$assocRegion = $assocPersister->getCacheRegion();

// Handle *-to-one associations
if ($assoc->isToOne()) {
$assocIdentifier = $this->uow->getEntityIdentifier($assocValue);
$assocIdentifier = $uow->getEntityIdentifier($assocValue);
$entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier);

if (! $this->uow->isUninitializedObject($assocValue) && ($key->cacheMode & Cache::MODE_REFRESH) || ! $assocRegion->contains($entityKey)) {
if (! $uow->isUninitializedObject($assocValue) && ($key->cacheMode & Cache::MODE_REFRESH) || ! $assocRegion->contains($entityKey)) {
// Entity put fail
if (! $assocPersister->storeEntityCache($assocValue, $entityKey)) {
return null;
Expand All @@ -323,7 +323,7 @@ private function storeAssociationCache(QueryCacheKey $key, AssociationMapping $a
$list = [];

foreach ($assocValue as $assocItemIndex => $assocItem) {
$assocIdentifier = $this->uow->getEntityIdentifier($assocItem);
$assocIdentifier = $uow->getEntityIdentifier($assocItem);
$entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier);

if (($key->cacheMode & Cache::MODE_REFRESH) || ! $assocRegion->contains($entityKey)) {
Expand Down
18 changes: 11 additions & 7 deletions src/Cache/Persister/Entity/AbstractEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

abstract class AbstractEntityPersister implements CachedEntityPersister
{
/** @deprecated get the unit of work with $this->em->getUnitOfWork() instead */
protected UnitOfWork $uow;
protected ClassMetadataFactory $metadataFactory;

Expand All @@ -57,7 +58,7 @@ abstract class AbstractEntityPersister implements CachedEntityPersister
public function __construct(
protected EntityPersister $persister,
protected Region $region,
EntityManagerInterface $em,
protected EntityManagerInterface $em,
protected ClassMetadata $class,
) {
$configuration = $em->getConfiguration();
Expand Down Expand Up @@ -190,10 +191,11 @@ private function storeJoinedAssociations(object $entity): void
continue;
}

$assocId = $this->uow->getEntityIdentifier($assocEntity);
$uow = $this->em->getUnitOfWork();
$assocId = $uow->getEntityIdentifier($assocEntity);
$assocMetadata = $this->metadataFactory->getMetadataFor($assoc->targetEntity);
$assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId);
$assocPersister = $this->uow->getEntityPersister($assoc->targetEntity);
$assocPersister = $uow->getEntityPersister($assoc->targetEntity);

$assocPersister->storeEntityCache($assocEntity, $assocKey);
}
Expand Down Expand Up @@ -465,14 +467,15 @@ public function loadManyToManyCollection(
object $sourceEntity,
PersistentCollection $collection,
): array {
$persister = $this->uow->getCollectionPersister($assoc);
$uow = $this->em->getUnitOfWork();
$persister = $uow->getCollectionPersister($assoc);
$hasCache = ($persister instanceof CachedPersister);

if (! $hasCache) {
return $this->persister->loadManyToManyCollection($assoc, $sourceEntity, $collection);
}

$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
$ownerId = $uow->getEntityIdentifier($collection->getOwner());
$key = $this->buildCollectionCacheKey($assoc, $ownerId);
$list = $persister->loadCollectionCache($collection, $key);

Expand All @@ -496,14 +499,15 @@ public function loadOneToManyCollection(
object $sourceEntity,
PersistentCollection $collection,
): mixed {
$persister = $this->uow->getCollectionPersister($assoc);
$uow = $this->em->getUnitOfWork();
$persister = $uow->getCollectionPersister($assoc);
$hasCache = ($persister instanceof CachedPersister);

if (! $hasCache) {
return $this->persister->loadOneToManyCollection($assoc, $sourceEntity, $collection);
}

$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
$ownerId = $uow->getEntityIdentifier($collection->getOwner());
$key = $this->buildCollectionCacheKey($assoc, $ownerId);
$list = $persister->loadCollectionCache($collection, $key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function afterTransactionRolledBack(): void

public function delete(object $entity): bool
{
$key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
$key = new EntityCacheKey($this->class->rootEntityName, $this->em->getUnitOfWork()->getEntityIdentifier($entity));
$deleted = $this->persister->delete($entity);

if ($deleted) {
Expand All @@ -71,7 +71,7 @@ public function update(object $entity): void
private function updateCache(object $entity, bool $isChanged): bool
{
$class = $this->metadataFactory->getMetadataFor($entity::class);
$key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity));
$key = new EntityCacheKey($class->rootEntityName, $this->em->getUnitOfWork()->getEntityIdentifier($entity));
$entry = $this->hydrator->buildCacheEntry($class, $key, $entity);
$cached = $this->region->put($key, $entry);
$isChanged = $isChanged || $cached;
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/Persister/Entity/ReadWriteCachedEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function afterTransactionRolledBack(): void

public function delete(object $entity): bool
{
$key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
$key = new EntityCacheKey($this->class->rootEntityName, $this->em->getUnitOfWork()->getEntityIdentifier($entity));
$lock = $this->region->lock($key);
$deleted = $this->persister->delete($entity);

Expand All @@ -88,7 +88,7 @@ public function delete(object $entity): bool

public function update(object $entity): void
{
$key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
$key = new EntityCacheKey($this->class->rootEntityName, $this->em->getUnitOfWork()->getEntityIdentifier($entity));
$lock = $this->region->lock($key);

$this->persister->update($entity);
Expand Down
4 changes: 2 additions & 2 deletions src/Decorator/EntityManagerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getReference(string $entityName, mixed $id): object|null

public function close(): void
{
$this->wrapped->close();
$this->wrapped->clear();
}

public function lock(object $entity, LockMode|int $lockMode, DateTimeInterface|int|null $lockVersion = null): void
Expand Down Expand Up @@ -134,7 +134,7 @@ public function getConfiguration(): Configuration

public function isOpen(): bool
{
return $this->wrapped->isOpen();
return true;
}

public function getUnitOfWork(): UnitOfWork
Expand Down
Loading

0 comments on commit c963b1a

Please sign in to comment.