Skip to content

Commit c963b1a

Browse files
committed
Port d05e8e8
The original commit has been made backward-compatible, and some properties of the entity manager have been marked as readonly. Closes #5933
1 parent 37946d3 commit c963b1a

15 files changed

+113
-115
lines changed

UPGRADE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# Upgrade to 3.3
2+
3+
## Deprecated closed status for EntityManager
4+
5+
It is always open now. As a consequence, the following methods, classes and
6+
properties have been deprecated and will be removed in 4.0:
7+
8+
- `EntityManagerInterface::isOpen()`. All implementations should return `true`.
9+
- `EntityManagerInterface::close()`. All implementations and callers should call `clear()` instead.
10+
- `AbstractEntityPersister::$uow`: get the unit of work from `AbstractEntityPersister::$em` instead
11+
- `AbstractCollectionPersister::$uow`: get the unit of work from `AbstractCollectionPersister::$em` instead
12+
13+
Mutating the following properties is deprecated and will not be possible in 4.0:
14+
15+
- `EntityManager::$config`
16+
- `EntityManager::$conn`
17+
- `EntityManager::$eventManager`
18+
- `EntityManager::$metadataFactory`
19+
- `EntityManager::$proxyFactory`
20+
- `EntityManager::$repositoryFactory`
21+
122
# Upgrade to 3.2
223

324
## Deprecate the `NotSupported` exception

psalm-baseline.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
<ArgumentTypeCoercion>
107107
<code><![CDATA[$cacheEntry]]></code>
108108
</ArgumentTypeCoercion>
109+
<DeprecatedProperty>
110+
<code><![CDATA[$this->uow]]></code>
111+
</DeprecatedProperty>
109112
<NoInterfaceProperties>
110113
<code><![CDATA[$cacheEntry->class]]></code>
111114
</NoInterfaceProperties>
@@ -648,6 +651,11 @@
648651
<code><![CDATA[[$this->unwrap(), 'add']]]></code>
649652
</UndefinedMethod>
650653
</file>
654+
<file src="src/Persisters/Collection/AbstractCollectionPersister.php">
655+
<DeprecatedProperty>
656+
<code><![CDATA[$this->uow]]></code>
657+
</DeprecatedProperty>
658+
</file>
651659
<file src="src/Persisters/Collection/ManyToManyPersister.php">
652660
<PossiblyNullArgument>
653661
<code><![CDATA[$collection->getOwner()]]></code>

src/Cache/DefaultQueryCache.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Doctrine\ORM\PersistentCollection;
1717
use Doctrine\ORM\Query;
1818
use Doctrine\ORM\Query\ResultSetMapping;
19-
use Doctrine\ORM\UnitOfWork;
2019

2120
use function array_map;
2221
use function array_shift;
@@ -32,7 +31,6 @@
3231
*/
3332
class DefaultQueryCache implements QueryCache
3433
{
35-
private readonly UnitOfWork $uow;
3634
private readonly QueryCacheValidator $validator;
3735
protected CacheLogger|null $cacheLogger = null;
3836

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

48-
$this->uow = $em->getUnitOfWork();
4946
$this->cacheLogger = $cacheConfig->getCacheLogger();
5047
$this->validator = $cacheConfig->getQueryValidator();
5148
}
@@ -74,7 +71,8 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
7471
$result = [];
7572
$entityName = reset($rsm->aliasMap);
7673
$hasRelation = ! empty($rsm->relationMap);
77-
$persister = $this->uow->getEntityPersister($entityName);
74+
$uow = $this->em->getUnitOfWork();
75+
$persister = $uow->getEntityPersister($entityName);
7876
assert($persister instanceof CachedEntityPersister);
7977

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

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

105103
continue;
106104
}
107105

108106
$data = $entityEntry->data;
109107

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

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

124-
$this->uow->hydrationComplete();
122+
$uow->hydrationComplete();
125123

126124
return null;
127125
}
128126

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

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

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

152-
$this->uow->hydrationComplete();
150+
$uow->hydrationComplete();
153151

154152
return null;
155153
}
156154

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

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

@@ -185,10 +183,10 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
185183
}
186184
}
187185

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

191-
$this->uow->hydrationComplete();
189+
$uow->hydrationComplete();
192190

193191
return $result;
194192
}
@@ -217,7 +215,8 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
217215
$data = [];
218216
$entityName = reset($rsm->aliasMap);
219217
$rootAlias = key($rsm->aliasMap);
220-
$persister = $this->uow->getEntityPersister($entityName);
218+
$uow = $this->em->getUnitOfWork();
219+
$persister = $uow->getEntityPersister($entityName);
221220

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

231230
foreach ($result as $index => $entity) {
232-
$identifier = $this->uow->getEntityIdentifier($entity);
231+
$identifier = $uow->getEntityIdentifier($entity);
233232
$entityKey = new EntityCacheKey($cm->rootEntityName, $identifier);
234233

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

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

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

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

329329
if (($key->cacheMode & Cache::MODE_REFRESH) || ! $assocRegion->contains($entityKey)) {

src/Cache/Persister/Entity/AbstractEntityPersister.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

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

@@ -57,7 +58,7 @@ abstract class AbstractEntityPersister implements CachedEntityPersister
5758
public function __construct(
5859
protected EntityPersister $persister,
5960
protected Region $region,
60-
EntityManagerInterface $em,
61+
protected EntityManagerInterface $em,
6162
protected ClassMetadata $class,
6263
) {
6364
$configuration = $em->getConfiguration();
@@ -190,10 +191,11 @@ private function storeJoinedAssociations(object $entity): void
190191
continue;
191192
}
192193

193-
$assocId = $this->uow->getEntityIdentifier($assocEntity);
194+
$uow = $this->em->getUnitOfWork();
195+
$assocId = $uow->getEntityIdentifier($assocEntity);
194196
$assocMetadata = $this->metadataFactory->getMetadataFor($assoc->targetEntity);
195197
$assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId);
196-
$assocPersister = $this->uow->getEntityPersister($assoc->targetEntity);
198+
$assocPersister = $uow->getEntityPersister($assoc->targetEntity);
197199

198200
$assocPersister->storeEntityCache($assocEntity, $assocKey);
199201
}
@@ -465,14 +467,15 @@ public function loadManyToManyCollection(
465467
object $sourceEntity,
466468
PersistentCollection $collection,
467469
): array {
468-
$persister = $this->uow->getCollectionPersister($assoc);
470+
$uow = $this->em->getUnitOfWork();
471+
$persister = $uow->getCollectionPersister($assoc);
469472
$hasCache = ($persister instanceof CachedPersister);
470473

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

475-
$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
478+
$ownerId = $uow->getEntityIdentifier($collection->getOwner());
476479
$key = $this->buildCollectionCacheKey($assoc, $ownerId);
477480
$list = $persister->loadCollectionCache($collection, $key);
478481

@@ -496,14 +499,15 @@ public function loadOneToManyCollection(
496499
object $sourceEntity,
497500
PersistentCollection $collection,
498501
): mixed {
499-
$persister = $this->uow->getCollectionPersister($assoc);
502+
$uow = $this->em->getUnitOfWork();
503+
$persister = $uow->getCollectionPersister($assoc);
500504
$hasCache = ($persister instanceof CachedPersister);
501505

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

506-
$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
510+
$ownerId = $uow->getEntityIdentifier($collection->getOwner());
507511
$key = $this->buildCollectionCacheKey($assoc, $ownerId);
508512
$list = $persister->loadCollectionCache($collection, $key);
509513

src/Cache/Persister/Entity/NonStrictReadWriteCachedEntityPersister.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function afterTransactionRolledBack(): void
4949

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

5555
if ($deleted) {
@@ -71,7 +71,7 @@ public function update(object $entity): void
7171
private function updateCache(object $entity, bool $isChanged): bool
7272
{
7373
$class = $this->metadataFactory->getMetadataFor($entity::class);
74-
$key = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity));
74+
$key = new EntityCacheKey($class->rootEntityName, $this->em->getUnitOfWork()->getEntityIdentifier($entity));
7575
$entry = $this->hydrator->buildCacheEntry($class, $key, $entity);
7676
$cached = $this->region->put($key, $entry);
7777
$isChanged = $isChanged || $cached;

src/Cache/Persister/Entity/ReadWriteCachedEntityPersister.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function afterTransactionRolledBack(): void
6666

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

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

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

9494
$this->persister->update($entity);

src/Decorator/EntityManagerDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function getReference(string $entityName, mixed $id): object|null
104104

105105
public function close(): void
106106
{
107-
$this->wrapped->close();
107+
$this->wrapped->clear();
108108
}
109109

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

135135
public function isOpen(): bool
136136
{
137-
return $this->wrapped->isOpen();
137+
return true;
138138
}
139139

140140
public function getUnitOfWork(): UnitOfWork

0 commit comments

Comments
 (0)