1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fwk |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>. |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
23
|
|
|
* |
24
|
|
|
* PHP Version 5.3 |
25
|
|
|
* |
26
|
|
|
* @category Database |
27
|
|
|
* @package Fwk\Db |
28
|
|
|
* @author Julien Ballestracci <[email protected]> |
29
|
|
|
* @copyright 2011-2012 Julien Ballestracci <[email protected]> |
30
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
31
|
|
|
* @link http://www.phpfwk.com |
32
|
|
|
*/ |
33
|
|
|
namespace Fwk\Db; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Finder |
37
|
|
|
* |
38
|
|
|
* Utility class to "navigate" within a database table. |
39
|
|
|
* |
40
|
|
|
* @category Library |
41
|
|
|
* @package Fwk\Db |
42
|
|
|
* @author Julien Ballestracci <[email protected]> |
43
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
44
|
|
|
* @link http://www.phpfwk.com |
45
|
|
|
*/ |
46
|
|
|
class Finder |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* Results |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $resultSet; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Table to navigate |
57
|
|
|
* |
58
|
|
|
* @var Table |
59
|
|
|
*/ |
60
|
|
|
protected $table; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Current Connection |
64
|
|
|
* |
65
|
|
|
* @var Connection |
66
|
|
|
*/ |
67
|
|
|
protected $connection; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Current query |
71
|
|
|
* |
72
|
|
|
* @var Query |
73
|
|
|
*/ |
74
|
|
|
protected $query; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Query parameters |
78
|
|
|
* |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
protected $params; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Entity class name |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
protected $entity; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* List of entity listeners |
92
|
|
|
* |
93
|
|
|
* @array |
94
|
|
|
*/ |
95
|
|
|
protected $listeners; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Constructor |
99
|
|
|
* |
100
|
|
|
* @param Table $table Main table to query |
101
|
|
|
* @param Connection $connection Actual Connection |
|
|
|
|
102
|
|
|
* |
103
|
|
|
* @return void |
|
|
|
|
104
|
|
|
*/ |
105
|
|
|
public function __construct(Table $table, Connection $connection = null) |
106
|
|
|
{ |
107
|
|
|
$query = Query::factory() |
108
|
|
|
->select() |
109
|
|
|
->from(sprintf('%s %s', $table->getName(), 'f')); |
110
|
|
|
|
111
|
|
|
$this->table = $table; |
112
|
|
|
|
113
|
|
|
if (null !== $connection) { |
114
|
|
|
$this->setConnection($connection); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->query = $query; |
118
|
|
|
$this->params = array(); |
119
|
|
|
$this->setEntity( |
120
|
|
|
$table->getDefaultEntity(), |
121
|
|
|
$table->getDefaultEntityListeners() |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Defines a connection |
127
|
|
|
* |
128
|
|
|
* @param Connection $connection Actual Connection |
129
|
|
|
* |
130
|
|
|
* @return Finder |
131
|
|
|
*/ |
132
|
|
|
public function setConnection(Connection $connection) |
133
|
|
|
{ |
134
|
|
|
$this->connection = $connection; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns current connection |
141
|
|
|
* |
142
|
|
|
* @throws Exception if no connection is defined |
143
|
|
|
* @return Connection |
144
|
|
|
*/ |
145
|
|
|
public function getConnection() |
146
|
|
|
{ |
147
|
|
|
if (!isset($this->connection)) { |
148
|
|
|
throw new Exception('No connection is defined'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->connection; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Find one or more results according to the list of $identifiers. |
156
|
|
|
* |
157
|
|
|
* @param array $identifiers List of columns to search |
158
|
|
|
* |
159
|
|
|
* @throws Exception if Query already done |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function find(array $identifiers) |
163
|
|
|
{ |
164
|
|
|
if (isset($this->resultSet)) { |
165
|
|
|
throw new Exception('Finder already executed'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->query->where('1 = 1'); |
169
|
|
|
$this->query->entity($this->getEntity()); |
170
|
|
|
|
171
|
|
|
foreach ($identifiers as $key => $value) { |
172
|
|
|
$this->query->andWhere(sprintf('f.%s = ?', $key)); |
173
|
|
|
$this->params[] = $value; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->getResultSet(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Finds one entry according to identifiers. |
181
|
|
|
* |
182
|
|
|
* @param mixed $identifiers List of/single identifiers |
183
|
|
|
* |
184
|
|
|
* @throws Exception if Query already done |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
public function one($identifiers) |
188
|
|
|
{ |
189
|
|
|
if (isset($this->resultSet)) { |
190
|
|
|
throw new Exception('Finder already executed'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$ids = $this->table->getIdentifiersKeys(); |
194
|
|
|
if (!is_array($identifiers)) { |
195
|
|
|
if (count($ids) === 1) { |
196
|
|
|
$identifiers = array($ids[0] => $identifiers); |
197
|
|
|
} else { |
198
|
|
|
$identifiers = array($identifiers); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->query->where('1 = 1'); |
203
|
|
|
$this->query->entity($this->getEntity(), $this->listeners); |
204
|
|
|
|
205
|
|
|
foreach ($ids as $key) { |
206
|
|
|
if (!isset($identifiers[$key])) { |
207
|
|
|
throw new Exceptions\MissingIdentifierException( |
208
|
|
|
sprintf('Missing required identifier "%s"', $key) |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$this->query->andWhere(sprintf('f.%s = ?', $key)); |
213
|
|
|
$this->params[] = $identifiers[$key]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$res = $this->getResultSet(); |
217
|
|
|
if (count($res) >= 1) { |
218
|
|
|
return $res[0]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Fetches all entries from the table |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
public function all() |
230
|
|
|
{ |
231
|
|
|
$this->query->entity($this->getEntity()); |
232
|
|
|
|
233
|
|
|
return $this->getResultSet(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Executes the query (if required) and returns the result set. |
238
|
|
|
* |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
|
|
public function getResultSet() |
242
|
|
|
{ |
243
|
|
|
if (!isset($this->resultSet)) { |
244
|
|
|
$this->resultSet = (array)$this->getConnection()->execute( |
245
|
|
|
$this->query, |
246
|
|
|
$this->params |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this->resultSet; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Entity class name |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
public function getEntity() |
259
|
|
|
{ |
260
|
|
|
return $this->entity; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* List of entity listeners |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
|
|
public function getListeners() |
269
|
|
|
{ |
270
|
|
|
return $this->listeners; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Defines the entity that should be returned by this Finder |
275
|
|
|
* |
276
|
|
|
* @param string $entity The entity class name |
277
|
|
|
* @param array $listeners List of listeners to be used with this entity |
278
|
|
|
* (table defaults are overriden) |
279
|
|
|
* |
280
|
|
|
* @return Finder |
281
|
|
|
*/ |
282
|
|
|
public function setEntity($entity, array $listeners = array()) |
283
|
|
|
{ |
284
|
|
|
$this->entity = $entity; |
285
|
|
|
$this->listeners = $listeners; |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
} |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.