1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\ORM\Cache; |
||
6 | |||
7 | use Doctrine\ORM\Cache\Persister\CachedPersister; |
||
8 | use Doctrine\ORM\EntityManagerInterface; |
||
9 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
10 | use Doctrine\ORM\PersistentCollection; |
||
11 | use Doctrine\ORM\Query; |
||
12 | use Doctrine\ORM\UnitOfWork; |
||
13 | |||
14 | /** |
||
15 | * Default hydrator cache for collections |
||
16 | */ |
||
17 | class DefaultCollectionHydrator implements CollectionHydrator |
||
18 | { |
||
19 | /** @var EntityManagerInterface */ |
||
20 | private $em; |
||
21 | |||
22 | /** @var UnitOfWork */ |
||
23 | private $uow; |
||
24 | |||
25 | /** @var mixed[] */ |
||
26 | private static $hints = [Query::HINT_CACHE_ENABLED => true]; |
||
27 | |||
28 | /** |
||
29 | * @param EntityManagerInterface $em The entity manager. |
||
30 | */ |
||
31 | 119 | public function __construct(EntityManagerInterface $em) |
|
32 | { |
||
33 | 119 | $this->em = $em; |
|
34 | 119 | $this->uow = $em->getUnitOfWork(); |
|
35 | 119 | } |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 45 | public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection) |
|
41 | { |
||
42 | 45 | $data = []; |
|
43 | |||
44 | 45 | foreach ($collection as $index => $entity) { |
|
45 | 45 | $data[$index] = new EntityCacheKey($metadata->getRootClassName(), $this->uow->getEntityIdentifier($entity)); |
|
46 | } |
||
47 | |||
48 | 45 | return new CollectionCacheEntry($data); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 16 | public function loadCacheEntry( |
|
55 | ClassMetadata $metadata, |
||
56 | CollectionCacheKey $key, |
||
57 | CollectionCacheEntry $entry, |
||
58 | PersistentCollection $collection |
||
59 | ) { |
||
60 | /** @var CachedPersister $targetPersister */ |
||
61 | 16 | $association = $metadata->getProperty($key->association); |
|
62 | 16 | $targetPersister = $this->uow->getEntityPersister($association->getTargetEntity()); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
63 | 16 | $targetRegion = $targetPersister->getCacheRegion(); |
|
64 | 16 | $list = []; |
|
65 | |||
66 | 16 | $entityEntries = $targetRegion->getMultiple($entry); |
|
67 | |||
68 | 16 | if ($entityEntries === null) { |
|
69 | 1 | return null; |
|
70 | } |
||
71 | |||
72 | /** @var EntityCacheEntry[] $entityEntries */ |
||
73 | 16 | foreach ($entityEntries as $index => $entityEntry) { |
|
74 | 14 | $data = $entityEntry->resolveAssociationEntries($this->em); |
|
75 | |||
76 | 14 | $entity = $this->uow->createEntity($entityEntry->class, $data, self::$hints); |
|
77 | |||
78 | 14 | $collection->hydrateSet($index, $entity); |
|
79 | |||
80 | 14 | $list[$index] = $entity; |
|
81 | } |
||
82 | |||
83 | 16 | $this->uow->hydrationComplete(); |
|
84 | |||
85 | 16 | return $list; |
|
86 | } |
||
87 | } |
||
88 |