|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/orm package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Orm\Repository\QueryObject; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Database\Sql\Select; |
|
13
|
|
|
use Slick\Orm\Entity\EntityCollection; |
|
14
|
|
|
use Slick\Orm\EntityInterface; |
|
15
|
|
|
use Slick\Orm\Event\EntityAdded; |
|
16
|
|
|
use Slick\Orm\Event\EntityRemoved; |
|
17
|
|
|
use Slick\Orm\RepositoryInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* QueryObject |
|
21
|
|
|
* |
|
22
|
|
|
* @package Slick\Orm\Repository |
|
23
|
|
|
* @author Filipe Silva <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class QueryObject extends Select implements QueryObjectInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var RepositoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $repository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* For triggering events |
|
35
|
|
|
*/ |
|
36
|
|
|
use SelectEventTriggers; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* QueryObject has a repository as a dependency. |
|
40
|
|
|
* |
|
41
|
|
|
* @param RepositoryInterface $repository |
|
42
|
|
|
*/ |
|
43
|
12 |
|
public function __construct(RepositoryInterface $repository) |
|
44
|
|
|
{ |
|
45
|
12 |
|
$this->repository = $repository; |
|
46
|
12 |
|
$this->adapter = $repository->getAdapter(); |
|
47
|
12 |
|
parent::__construct( |
|
48
|
12 |
|
$repository->getEntityDescriptor()->getTableName(), |
|
49
|
12 |
|
$repository->getEntityDescriptor()->getTableName().'.*' |
|
50
|
6 |
|
); |
|
51
|
12 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Retrieve all records matching this select query |
|
55
|
|
|
* |
|
56
|
|
|
* @return \Slick\Database\RecordList |
|
57
|
|
|
*/ |
|
58
|
6 |
|
public function all() |
|
59
|
|
|
{ |
|
60
|
6 |
|
$cid = $this->getId($this); |
|
61
|
6 |
|
$collection = $this->repository |
|
62
|
6 |
|
->getCollectionsMap() |
|
63
|
6 |
|
->get($cid, false); |
|
64
|
|
|
|
|
65
|
6 |
|
if (false === $collection) { |
|
66
|
4 |
|
$this->triggerBeforeSelect( |
|
67
|
2 |
|
$this, |
|
68
|
4 |
|
$this->getRepository()->getEntityDescriptor() |
|
69
|
2 |
|
); |
|
70
|
4 |
|
$data = $this->adapter->query($this, $this->getParameters()); |
|
71
|
4 |
|
$collection = $this->repository->getEntityMapper() |
|
72
|
4 |
|
->createFrom($data); |
|
73
|
4 |
|
$this->triggerAfterSelect( |
|
74
|
2 |
|
$data, |
|
75
|
|
|
$collection |
|
|
|
|
|
|
76
|
2 |
|
); |
|
77
|
4 |
|
$collection->setId($cid); |
|
78
|
4 |
|
$collection->getEmitter() |
|
|
|
|
|
|
79
|
4 |
|
->addListener( |
|
80
|
4 |
|
EntityAdded::ACTION_ADD, |
|
81
|
4 |
|
[$this, 'updateCollection'] |
|
82
|
2 |
|
); |
|
83
|
4 |
|
$collection->getEmitter() |
|
84
|
4 |
|
->addListener( |
|
85
|
4 |
|
EntityRemoved::ACTION_REMOVE, |
|
86
|
4 |
|
[$this, 'updateCollection'] |
|
87
|
2 |
|
); |
|
88
|
4 |
|
$this->repository->getCollectionsMap()->set($cid, $collection); |
|
|
|
|
|
|
89
|
4 |
|
$this->updateIdentityMap($collection); |
|
|
|
|
|
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
6 |
|
return $collection; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Handles collection add event and updates the cache |
|
97
|
|
|
* |
|
98
|
|
|
* @param EntityAdded $event |
|
99
|
|
|
*/ |
|
100
|
|
|
public function updateCollection(EntityAdded $event) |
|
101
|
|
|
{ |
|
102
|
|
|
$collection = $event->getCollection(); |
|
103
|
|
|
if ($collection->getId()) { |
|
104
|
|
|
$this->repository->getCollectionsMap() |
|
105
|
|
|
->set($collection->getId(), $collection); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Retrieve first record matching this select query |
|
111
|
|
|
* |
|
112
|
|
|
* @return EntityInterface|null |
|
113
|
|
|
*/ |
|
114
|
2 |
|
public function first() |
|
115
|
|
|
{ |
|
116
|
2 |
|
$sql = clone($this); |
|
117
|
2 |
|
$sql->limit(1); |
|
118
|
2 |
|
$cid = $this->getId($sql); |
|
119
|
|
|
|
|
120
|
2 |
|
$collection = $this->repository |
|
121
|
2 |
|
->getCollectionsMap() |
|
122
|
2 |
|
->get($cid, false); |
|
123
|
|
|
|
|
124
|
2 |
|
if (false === $collection) { |
|
125
|
2 |
|
$this->triggerBeforeSelect( |
|
126
|
1 |
|
$sql, |
|
127
|
2 |
|
$this->getRepository()->getEntityDescriptor() |
|
128
|
1 |
|
); |
|
129
|
2 |
|
$data = $this->adapter->query($sql, $sql->getParameters()); |
|
130
|
2 |
|
$collection = $this->repository->getEntityMapper() |
|
131
|
2 |
|
->createFrom($data); |
|
132
|
2 |
|
$this->triggerAfterSelect( |
|
133
|
1 |
|
$data, |
|
134
|
|
|
$collection |
|
|
|
|
|
|
135
|
1 |
|
); |
|
136
|
2 |
|
$this->repository->getCollectionsMap()->set($cid, $collection); |
|
|
|
|
|
|
137
|
2 |
|
$this->updateIdentityMap($collection); |
|
|
|
|
|
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
return $collection->isEmpty() ? null : $collection[0]; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Gets the id for this query |
|
145
|
|
|
* |
|
146
|
|
|
* @param QueryObjectInterface $query |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
8 |
|
protected function getId(QueryObjectInterface $query) |
|
151
|
|
|
{ |
|
152
|
8 |
|
$str = $query->getQueryString(); |
|
153
|
8 |
|
$search = array_keys($query->getParameters()); |
|
154
|
8 |
|
$values = array_values($query->getParameters()); |
|
155
|
8 |
|
return str_replace($search, $values, $str); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Registers every entity in collection to the repository identity map |
|
160
|
|
|
* |
|
161
|
|
|
* @param EntityCollection $collection |
|
162
|
|
|
* |
|
163
|
|
|
* @return self |
|
164
|
|
|
*/ |
|
165
|
6 |
|
protected function updateIdentityMap(EntityCollection $collection) |
|
166
|
|
|
{ |
|
167
|
6 |
|
if ($collection->isEmpty()) { |
|
168
|
2 |
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
4 |
|
foreach ($collection as $entity) |
|
172
|
|
|
{ |
|
173
|
4 |
|
$this->repository->getIdentityMap()->set($entity); |
|
174
|
2 |
|
} |
|
175
|
4 |
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Returns the repository that is using this query object |
|
180
|
|
|
* |
|
181
|
|
|
* @return RepositoryInterface |
|
182
|
|
|
*/ |
|
183
|
10 |
|
public function getRepository() |
|
184
|
|
|
{ |
|
185
|
10 |
|
return $this->repository; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Gets current entity class name |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
6 |
|
public function getEntityClassName() |
|
194
|
|
|
{ |
|
195
|
6 |
|
return $this->repository->getEntityDescriptor()->className(); |
|
196
|
|
|
} |
|
197
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.