|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineElastic\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\JoinColumn; |
|
6
|
|
|
use Doctrine\ORM\Mapping\JoinColumns; |
|
7
|
|
|
use Doctrine\ORM\Mapping\ManyToOne; |
|
8
|
|
|
use DoctrineElastic\Event\QueryEventArgs; |
|
9
|
|
|
use DoctrineElastic\Hydrate\AnnotationEntityHydrator; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Query events main handler for this extension |
|
13
|
|
|
* |
|
14
|
|
|
* @author Ands |
|
15
|
|
|
*/ |
|
16
|
|
|
class QueryListener { |
|
17
|
|
|
|
|
18
|
|
|
/** @var AnnotationEntityHydrator */ |
|
19
|
|
|
protected $hydrator; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() { |
|
22
|
|
|
$this->hydrator = new AnnotationEntityHydrator(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function beforeQuery(QueryEventArgs $eventArgs) { |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param QueryEventArgs $eventArgs |
|
31
|
|
|
*/ |
|
32
|
|
|
public function postQuery(QueryEventArgs $eventArgs) { |
|
33
|
|
|
$results = $eventArgs->getResults(); |
|
34
|
|
|
$entityManager = $eventArgs->getEntityManager(); |
|
35
|
|
|
$targetEntity = $eventArgs->getTargetEntity(); |
|
36
|
|
|
|
|
37
|
|
|
if (!empty($results) && boolval($entityManager) && boolval($targetEntity)) { |
|
38
|
|
|
$this->executeRelationshipQueries($eventArgs); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Makes a heavy work, simulating a doctrine 1-n entity relationship |
|
44
|
|
|
* This recourse must be used cautiously. Check if your application really needs that. |
|
45
|
|
|
* |
|
46
|
|
|
* @param QueryEventArgs $eventArgs |
|
47
|
|
|
*/ |
|
48
|
|
|
private function executeRelationshipQueries(QueryEventArgs $eventArgs) { |
|
49
|
|
|
$targetClass = $eventArgs->getTargetEntity(); |
|
50
|
|
|
$entityManager = $eventArgs->getEntityManager(); |
|
51
|
|
|
$results = $eventArgs->getResults(); |
|
52
|
|
|
|
|
53
|
|
|
/** @var ManyToOne[] $manyToOnes */ |
|
54
|
|
|
$manyToOnes = $this->hydrator->extractSpecAnnotations($targetClass, ManyToOne::class); |
|
55
|
|
|
/** @var JoinColumns[] $joinsColumns */ |
|
56
|
|
|
$joinsColumns = $this->hydrator->extractSpecAnnotations($targetClass, JoinColumns::class); |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
foreach ($manyToOnes as $propName => $mto) { |
|
|
|
|
|
|
59
|
|
|
if (!isset($joinsColumns[$propName])) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
foreach ($joinsColumns as $joinsColumn) { |
|
64
|
|
|
/** @var JoinColumn[] $joinColumns */ |
|
65
|
|
|
$joinColumns = $joinsColumn->value; |
|
66
|
|
|
foreach ($joinColumns as $joinColumn) { |
|
67
|
|
|
$colunmName = AnnotationEntityHydrator::camelizeString($joinColumn->referencedColumnName); |
|
68
|
|
|
foreach ($results as $key => $result) { |
|
69
|
|
|
if (property_exists(get_class($result), $colunmName)) { |
|
70
|
|
|
$value = $this->hydrator->extract($result, $colunmName); |
|
71
|
|
|
$relObject = $entityManager->getRepository($mto->targetEntity) |
|
72
|
|
|
->findOneBy([$colunmName => $value]); |
|
73
|
|
|
$this->hydrator->hydrate($results[$key], [$colunmName => $relObject]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$eventArgs->setResults($results); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Fetch and inject the parent object to the child ones from query |
|
85
|
|
|
* |
|
86
|
|
|
* @param QueryEventArgs $eventArgs |
|
87
|
|
|
*/ |
|
88
|
|
|
private function insertParentsData(QueryEventArgs $eventArgs) { |
|
|
|
|
|
|
89
|
|
|
$targetClass = $eventArgs->getTargetEntity(); |
|
90
|
|
|
$entityManager = $eventArgs->getEntityManager(); |
|
91
|
|
|
$results = $eventArgs->getResults(); |
|
92
|
|
|
|
|
93
|
|
|
/** @var ManyToOne[] $manyToOnes */ |
|
94
|
|
|
$manyToOnes = $this->hydrator->extractSpecAnnotations($targetClass, ManyToOne::class); |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
foreach ($manyToOnes as $propName => $mto) { |
|
|
|
|
|
|
97
|
|
|
if (!isset($joinsColumns[$propName])) { |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
foreach ($joinsColumns as $joinsColumn) { |
|
102
|
|
|
/** @var JoinColumn[] $joinColumns */ |
|
103
|
|
|
$joinColumns = $joinsColumn->value; |
|
104
|
|
|
foreach ($joinColumns as $joinColumn) { |
|
105
|
|
|
$colunmName = AnnotationEntityHydrator::camelizeString($joinColumn->referencedColumnName); |
|
106
|
|
|
foreach ($results as $key => $result) { |
|
107
|
|
|
if (property_exists(get_class($result), $colunmName)) { |
|
108
|
|
|
$value = $this->hydrator->extract($result, $colunmName); |
|
109
|
|
|
$relObject = $entityManager->getRepository($mto->targetEntity) |
|
110
|
|
|
->findOneBy([$colunmName => $value]); |
|
111
|
|
|
$this->hydrator->hydrate($results[$key], [$colunmName => $relObject]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$eventArgs->setResults($results); |
|
119
|
|
|
} |
|
120
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.