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\RepositoryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* QueryObject |
19
|
|
|
* |
20
|
|
|
* @package Slick\Orm\Repository |
21
|
|
|
* @author Filipe Silva <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class QueryObject extends Select implements QueryObjectInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $repository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* For triggering events |
33
|
|
|
*/ |
34
|
|
|
use SelectEventTriggers; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* QueryObject has a repository as a dependency. |
38
|
|
|
* |
39
|
|
|
* @param RepositoryInterface $repository |
40
|
|
|
*/ |
41
|
12 |
|
public function __construct(RepositoryInterface $repository) |
42
|
|
|
{ |
43
|
12 |
|
$this->repository = $repository; |
44
|
12 |
|
$this->adapter = $repository->getAdapter(); |
45
|
12 |
|
parent::__construct( |
46
|
12 |
|
$repository->getEntityDescriptor()->getTableName(), |
47
|
12 |
|
$repository->getEntityDescriptor()->getTableName().'.*' |
48
|
12 |
|
); |
49
|
12 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieve all records matching this select query |
53
|
|
|
* |
54
|
|
|
* @return \Slick\Database\RecordList |
55
|
|
|
*/ |
56
|
6 |
|
public function all() |
57
|
|
|
{ |
58
|
6 |
|
$cid = $this->getId($this); |
59
|
6 |
|
$collection = $this->repository |
60
|
6 |
|
->getCollectionsMap() |
61
|
6 |
|
->get($cid, false); |
62
|
|
|
|
63
|
6 |
|
if (false === $collection) { |
64
|
4 |
|
$this->triggerBeforeSelect( |
65
|
4 |
|
$this, |
66
|
4 |
|
$this->getRepository()->getEntityDescriptor() |
67
|
4 |
|
); |
68
|
4 |
|
$data = $this->adapter->query($this, $this->getParameters()); |
69
|
4 |
|
$collection = $this->repository->getEntityMapper() |
70
|
4 |
|
->createFrom($data); |
71
|
4 |
|
$this->triggerAfterSelect( |
72
|
4 |
|
$data, |
73
|
|
|
$collection |
|
|
|
|
74
|
4 |
|
); |
75
|
4 |
|
$this->repository->getCollectionsMap()->set($cid, $collection); |
|
|
|
|
76
|
4 |
|
$this->updateIdentityMap($collection); |
|
|
|
|
77
|
4 |
|
} |
78
|
|
|
|
79
|
6 |
|
return $collection; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Retrieve first record matching this select query |
84
|
|
|
* |
85
|
|
|
* @return EntityInterface|null |
86
|
|
|
*/ |
87
|
2 |
|
public function first() |
88
|
|
|
{ |
89
|
2 |
|
$sql = clone($this); |
90
|
2 |
|
$sql->limit(1); |
91
|
2 |
|
$cid = $this->getId($sql); |
92
|
|
|
|
93
|
2 |
|
$collection = $this->repository |
94
|
2 |
|
->getCollectionsMap() |
95
|
2 |
|
->get($cid, false); |
96
|
|
|
|
97
|
2 |
|
if (false === $collection) { |
98
|
2 |
|
$this->triggerBeforeSelect( |
99
|
2 |
|
$sql, |
100
|
2 |
|
$this->getRepository()->getEntityDescriptor() |
101
|
2 |
|
); |
102
|
2 |
|
$data = $this->adapter->query($sql, $sql->getParameters()); |
103
|
2 |
|
$collection = $this->repository->getEntityMapper() |
104
|
2 |
|
->createFrom($data); |
105
|
2 |
|
$this->triggerAfterSelect( |
106
|
2 |
|
$data, |
107
|
|
|
$collection |
|
|
|
|
108
|
2 |
|
); |
109
|
2 |
|
$this->repository->getCollectionsMap()->set($cid, $collection); |
|
|
|
|
110
|
2 |
|
$this->updateIdentityMap($collection); |
|
|
|
|
111
|
2 |
|
} |
112
|
|
|
|
113
|
2 |
|
return $collection->isEmpty() |
|
|
|
|
114
|
2 |
|
? null |
115
|
2 |
|
: $collection[0]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets the id for this query |
120
|
|
|
* |
121
|
|
|
* @param QueryObjectInterface $query |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
8 |
|
protected function getId(QueryObjectInterface $query) |
126
|
|
|
{ |
127
|
8 |
|
$str = $query->getQueryString(); |
128
|
8 |
|
$search = array_keys($query->getParameters()); |
129
|
8 |
|
$values = array_values($query->getParameters()); |
130
|
8 |
|
return str_replace($search, $values, $str); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Registers every entity in collection to the repository identity map |
135
|
|
|
* |
136
|
|
|
* @param EntityCollection $collection |
137
|
|
|
* |
138
|
|
|
* @return self |
139
|
|
|
*/ |
140
|
6 |
|
protected function updateIdentityMap(EntityCollection $collection) |
141
|
|
|
{ |
142
|
6 |
|
if ($collection->isEmpty()) { |
143
|
2 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
4 |
|
foreach ($collection as $entity) |
147
|
|
|
{ |
148
|
4 |
|
$this->repository->getIdentityMap()->set($entity); |
149
|
4 |
|
} |
150
|
4 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the repository that is using this query object |
155
|
|
|
* |
156
|
|
|
* @return RepositoryInterface |
157
|
|
|
*/ |
158
|
10 |
|
public function getRepository() |
159
|
|
|
{ |
160
|
10 |
|
return $this->repository; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Gets current entity class name |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
6 |
|
public function getEntityClassName() |
169
|
|
|
{ |
170
|
6 |
|
return $this->repository->getEntityDescriptor()->className(); |
171
|
|
|
} |
172
|
|
|
} |
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.