16
16
use Doctrine \ORM \PersistentCollection ;
17
17
use Doctrine \ORM \Query ;
18
18
use Doctrine \ORM \Query \ResultSetMapping ;
19
- use Doctrine \ORM \UnitOfWork ;
20
19
21
20
use function array_map ;
22
21
use function array_shift ;
32
31
*/
33
32
class DefaultQueryCache implements QueryCache
34
33
{
35
- private readonly UnitOfWork $ uow ;
36
34
private readonly QueryCacheValidator $ validator ;
37
35
protected CacheLogger |null $ cacheLogger = null ;
38
36
@@ -45,7 +43,6 @@ public function __construct(
45
43
) {
46
44
$ cacheConfig = $ em ->getConfiguration ()->getSecondLevelCacheConfiguration ();
47
45
48
- $ this ->uow = $ em ->getUnitOfWork ();
49
46
$ this ->cacheLogger = $ cacheConfig ->getCacheLogger ();
50
47
$ this ->validator = $ cacheConfig ->getQueryValidator ();
51
48
}
@@ -74,7 +71,8 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
74
71
$ result = [];
75
72
$ entityName = reset ($ rsm ->aliasMap );
76
73
$ hasRelation = ! empty ($ rsm ->relationMap );
77
- $ persister = $ this ->uow ->getEntityPersister ($ entityName );
74
+ $ uow = $ this ->em ->getUnitOfWork ();
75
+ $ persister = $ uow ->getEntityPersister ($ entityName );
78
76
assert ($ persister instanceof CachedEntityPersister);
79
77
80
78
$ region = $ persister ->getCacheRegion ();
@@ -100,15 +98,15 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
100
98
$ this ->cacheLogger ?->entityCacheHit($ regionName , $ cacheKeys ->identifiers [$ index ]);
101
99
102
100
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 );
104
102
105
103
continue ;
106
104
}
107
105
108
106
$ data = $ entityEntry ->data ;
109
107
110
108
foreach ($ entry ['associations ' ] as $ name => $ assoc ) {
111
- $ assocPersister = $ this -> uow ->getEntityPersister ($ assoc ['targetEntity ' ]);
109
+ $ assocPersister = $ uow ->getEntityPersister ($ assoc ['targetEntity ' ]);
112
110
assert ($ assocPersister instanceof CachedEntityPersister);
113
111
114
112
$ assocRegion = $ assocPersister ->getCacheRegion ();
@@ -121,12 +119,12 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
121
119
if ($ assocEntry === null ) {
122
120
$ this ->cacheLogger ?->entityCacheMiss($ assocRegion ->getName (), $ assocKey );
123
121
124
- $ this -> uow ->hydrationComplete ();
122
+ $ uow ->hydrationComplete ();
125
123
126
124
return null ;
127
125
}
128
126
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 );
130
128
131
129
$ this ->cacheLogger ?->entityCacheHit($ assocRegion ->getName (), $ assocKey );
132
130
@@ -149,12 +147,12 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
149
147
if ($ assocEntry === null ) {
150
148
$ this ->cacheLogger ?->entityCacheMiss($ assocRegion ->getName (), $ assocKeys ->identifiers [$ assocIndex ]);
151
149
152
- $ this -> uow ->hydrationComplete ();
150
+ $ uow ->hydrationComplete ();
153
151
154
152
return null ;
155
153
}
156
154
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 );
158
156
159
157
$ collection ->hydrateSet ($ assocIndex , $ element );
160
158
@@ -185,10 +183,10 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
185
183
}
186
184
}
187
185
188
- $ result [$ index ] = $ this -> uow ->createEntity ($ entityEntry ->class , $ data , self ::$ hints );
186
+ $ result [$ index ] = $ uow ->createEntity ($ entityEntry ->class , $ data , self ::$ hints );
189
187
}
190
188
191
- $ this -> uow ->hydrationComplete ();
189
+ $ uow ->hydrationComplete ();
192
190
193
191
return $ result ;
194
192
}
@@ -217,7 +215,8 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
217
215
$ data = [];
218
216
$ entityName = reset ($ rsm ->aliasMap );
219
217
$ rootAlias = key ($ rsm ->aliasMap );
220
- $ persister = $ this ->uow ->getEntityPersister ($ entityName );
218
+ $ uow = $ this ->em ->getUnitOfWork ();
219
+ $ persister = $ uow ->getEntityPersister ($ entityName );
221
220
222
221
if (! $ persister instanceof CachedEntityPersister) {
223
222
throw NonCacheableEntity::fromEntity ($ entityName );
@@ -229,7 +228,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
229
228
assert ($ cm instanceof ClassMetadata);
230
229
231
230
foreach ($ result as $ index => $ entity ) {
232
- $ identifier = $ this -> uow ->getEntityIdentifier ($ entity );
231
+ $ identifier = $ uow ->getEntityIdentifier ($ entity );
233
232
$ entityKey = new EntityCacheKey ($ cm ->rootEntityName , $ identifier );
234
233
235
234
if (($ key ->cacheMode & Cache::MODE_REFRESH ) || ! $ region ->contains ($ entityKey )) {
@@ -296,16 +295,17 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
296
295
*/
297
296
private function storeAssociationCache (QueryCacheKey $ key , AssociationMapping $ assoc , mixed $ assocValue ): array |null
298
297
{
299
- $ assocPersister = $ this ->uow ->getEntityPersister ($ assoc ->targetEntity );
298
+ $ uow = $ this ->em ->getUnitOfWork ();
299
+ $ assocPersister = $ uow ->getEntityPersister ($ assoc ->targetEntity );
300
300
$ assocMetadata = $ assocPersister ->getClassMetadata ();
301
301
$ assocRegion = $ assocPersister ->getCacheRegion ();
302
302
303
303
// Handle *-to-one associations
304
304
if ($ assoc ->isToOne ()) {
305
- $ assocIdentifier = $ this -> uow ->getEntityIdentifier ($ assocValue );
305
+ $ assocIdentifier = $ uow ->getEntityIdentifier ($ assocValue );
306
306
$ entityKey = new EntityCacheKey ($ assocMetadata ->rootEntityName , $ assocIdentifier );
307
307
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 )) {
309
309
// Entity put fail
310
310
if (! $ assocPersister ->storeEntityCache ($ assocValue , $ entityKey )) {
311
311
return null ;
@@ -323,7 +323,7 @@ private function storeAssociationCache(QueryCacheKey $key, AssociationMapping $a
323
323
$ list = [];
324
324
325
325
foreach ($ assocValue as $ assocItemIndex => $ assocItem ) {
326
- $ assocIdentifier = $ this -> uow ->getEntityIdentifier ($ assocItem );
326
+ $ assocIdentifier = $ uow ->getEntityIdentifier ($ assocItem );
327
327
$ entityKey = new EntityCacheKey ($ assocMetadata ->rootEntityName , $ assocIdentifier );
328
328
329
329
if (($ key ->cacheMode & Cache::MODE_REFRESH ) || ! $ assocRegion ->contains ($ entityKey )) {
0 commit comments