|
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
|
52 |
|
public function __construct(EntityManagerInterface $em) |
|
32
|
|
|
{ |
|
33
|
52 |
|
$this->em = $em; |
|
34
|
52 |
|
$this->uow = $em->getUnitOfWork(); |
|
35
|
52 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection) |
|
41
|
|
|
{ |
|
42
|
|
|
$data = []; |
|
43
|
|
|
|
|
44
|
|
|
foreach ($collection as $index => $entity) { |
|
45
|
|
|
$data[$index] = new EntityCacheKey($metadata->getRootClassName(), $this->uow->getEntityIdentifier($entity)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return new CollectionCacheEntry($data); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function loadCacheEntry( |
|
55
|
|
|
ClassMetadata $metadata, |
|
56
|
|
|
CollectionCacheKey $key, |
|
57
|
|
|
CollectionCacheEntry $entry, |
|
58
|
|
|
PersistentCollection $collection |
|
59
|
|
|
) { |
|
60
|
|
|
/** @var CachedPersister $targetPersister */ |
|
61
|
1 |
|
$association = $metadata->getProperty($key->association); |
|
62
|
1 |
|
$targetPersister = $this->uow->getEntityPersister($association->getTargetEntity()); |
|
|
|
|
|
|
63
|
1 |
|
$targetRegion = $targetPersister->getCacheRegion(); |
|
|
|
|
|
|
64
|
1 |
|
$list = []; |
|
65
|
|
|
|
|
66
|
1 |
|
$entityEntries = $targetRegion->getMultiple($entry); |
|
67
|
|
|
|
|
68
|
1 |
|
if ($entityEntries === null) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** @var EntityCacheEntry[] $entityEntries */ |
|
73
|
1 |
|
foreach ($entityEntries as $index => $entityEntry) { |
|
74
|
1 |
|
$data = $entityEntry->resolveAssociationEntries($this->em); |
|
75
|
|
|
|
|
76
|
1 |
|
$entity = $this->uow->createEntity($entityEntry->class, $data, self::$hints); |
|
77
|
|
|
|
|
78
|
1 |
|
$collection->hydrateSet($index, $entity); |
|
79
|
|
|
|
|
80
|
1 |
|
$list[$index] = $entity; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
$this->uow->hydrationComplete(); |
|
84
|
|
|
|
|
85
|
1 |
|
return $list; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|