1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace StefanoTree\NestedSet\Adapter; |
6
|
|
|
|
7
|
|
|
use StefanoTree\NestedSet\NodeInfo; |
8
|
|
|
use StefanoTree\NestedSet\Options; |
9
|
|
|
use Zend_Db_Adapter_Abstract as ZendDbAdapter; |
10
|
|
|
use Zend_Db_Select as ZendDbSelect; |
11
|
|
|
|
12
|
|
|
class Zend1 extends AdapterAbstract implements AdapterInterface |
13
|
|
|
{ |
14
|
|
|
protected $dbAdapter; |
15
|
|
|
|
16
|
55 |
|
public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
17
|
|
|
{ |
18
|
55 |
|
$this->setOptions($options); |
19
|
55 |
|
$this->setDbAdapter($dbAdapter); |
20
|
55 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ZendDbAdapter $dbAdapter |
24
|
|
|
*/ |
25
|
55 |
|
protected function setDbAdapter(ZendDbAdapter $dbAdapter): void |
26
|
|
|
{ |
27
|
55 |
|
$this->dbAdapter = $dbAdapter; |
28
|
55 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return ZendDbAdapter |
32
|
|
|
*/ |
33
|
52 |
|
public function getDbAdapter(): ZendDbAdapter |
34
|
|
|
{ |
35
|
52 |
|
return $this->dbAdapter; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return base db select without any join, etc. |
40
|
|
|
* |
41
|
|
|
* @return ZendDbSelect |
42
|
|
|
*/ |
43
|
32 |
|
public function getBlankDbSelect(): ZendDbSelect |
44
|
|
|
{ |
45
|
32 |
|
return $this->getDbAdapter() |
46
|
32 |
|
->select() |
47
|
32 |
|
->from($this->getOptions()->getTableName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return default db select. Always new instance. |
52
|
|
|
* |
53
|
|
|
* @return ZendDbSelect |
54
|
|
|
*/ |
55
|
17 |
|
public function getDefaultDbSelect(): ZendDbSelect |
56
|
|
|
{ |
57
|
17 |
|
return $this->getDbSelectBuilder()(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
1 |
|
public function lockTree(): void |
64
|
|
|
{ |
65
|
1 |
|
$options = $this->getOptions(); |
66
|
|
|
|
67
|
1 |
|
$dbAdapter = $this->getDbAdapter(); |
68
|
|
|
|
69
|
1 |
|
$select = $this->getBlankDbSelect() |
70
|
1 |
|
->reset(\Zend_Db_Select::COLUMNS) |
71
|
1 |
|
->columns(array('i' => $options->getIdColumnName(true))) |
72
|
1 |
|
->forUpdate(true); |
73
|
|
|
|
74
|
1 |
|
$dbAdapter->fetchAll($select); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
1 |
|
public function beginTransaction(): void |
81
|
|
|
{ |
82
|
1 |
|
$this->getDbAdapter()->beginTransaction(); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
1 |
|
public function commitTransaction(): void |
89
|
|
|
{ |
90
|
1 |
|
$this->getDbAdapter()->commit(); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
1 |
|
public function rollbackTransaction(): void |
97
|
|
|
{ |
98
|
1 |
|
$this->getDbAdapter()->rollBack(); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
3 |
|
public function update($nodeId, array $data): void |
105
|
|
|
{ |
106
|
3 |
|
$options = $this->getOptions(); |
107
|
|
|
|
108
|
3 |
|
$dbAdapter = $this->getDbAdapter(); |
109
|
|
|
|
110
|
3 |
|
$data = $this->cleanData($data); |
111
|
|
|
|
112
|
|
|
$where = array( |
113
|
3 |
|
$dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
114
|
|
|
); |
115
|
3 |
|
$dbAdapter->update($options->getTableName(), $data, $where); |
116
|
3 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
3 |
|
public function insert(NodeInfo $nodeInfo, array $data) |
122
|
|
|
{ |
123
|
3 |
|
$options = $this->getOptions(); |
124
|
3 |
|
$dbAdapter = $this->getDbAdapter(); |
125
|
|
|
|
126
|
3 |
|
$data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
127
|
3 |
|
$data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
128
|
3 |
|
$data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
129
|
3 |
|
$data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
130
|
|
|
|
131
|
3 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
132
|
1 |
|
$data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
$dbAdapter->insert($options->getTableName(), $data); |
136
|
3 |
|
if ('' != $options->getSequenceName()) { |
137
|
|
|
$lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName()); |
138
|
|
|
} else { |
139
|
3 |
|
$lastGeneratedValue = $dbAdapter->lastInsertId(); |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
return $lastGeneratedValue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
2 |
|
public function delete($nodeId): void |
149
|
|
|
{ |
150
|
2 |
|
$options = $this->getOptions(); |
151
|
|
|
|
152
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
153
|
|
|
|
154
|
|
|
$where = array( |
155
|
2 |
|
$dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
156
|
|
|
); |
157
|
|
|
|
158
|
2 |
|
$dbAdapter->delete($options->getTableName(), $where); |
159
|
2 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
2 |
|
public function moveLeftIndexes($fromIndex, $shift, $scope = null): void |
165
|
|
|
{ |
166
|
2 |
|
$options = $this->getOptions(); |
167
|
|
|
|
168
|
2 |
|
if (0 == $shift) { |
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
173
|
2 |
|
$sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
174
|
2 |
|
.' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
175
|
2 |
|
.' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift' |
176
|
2 |
|
.' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' > :fromIndex'; |
177
|
|
|
|
178
|
2 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
179
|
1 |
|
$sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$binds = array( |
183
|
2 |
|
':shift' => $shift, |
184
|
2 |
|
':fromIndex' => $fromIndex, |
185
|
|
|
); |
186
|
2 |
|
$dbAdapter->prepare($sql)->execute($binds); |
187
|
2 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
2 |
|
public function moveRightIndexes($fromIndex, $shift, $scope = null): void |
193
|
|
|
{ |
194
|
2 |
|
$options = $this->getOptions(); |
195
|
|
|
|
196
|
2 |
|
if (0 == $shift) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
201
|
|
|
|
202
|
2 |
|
$sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
203
|
2 |
|
.' SET '.$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
204
|
2 |
|
.' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift' |
205
|
2 |
|
.' WHERE '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' > :fromIndex'; |
206
|
|
|
|
207
|
2 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
208
|
1 |
|
$sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$binds = array( |
212
|
2 |
|
':shift' => $shift, |
213
|
2 |
|
':fromIndex' => $fromIndex, |
214
|
|
|
); |
215
|
|
|
|
216
|
2 |
|
$dbAdapter->prepare($sql)->execute($binds); |
217
|
2 |
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
1 |
|
public function updateParentId($nodeId, $newParentId): void |
223
|
|
|
{ |
224
|
1 |
|
$options = $this->getOptions(); |
225
|
|
|
|
226
|
1 |
|
$dbAdapter = $this->getDbAdapter(); |
227
|
|
|
|
228
|
|
|
$bind = array( |
229
|
1 |
|
$options->getParentIdColumnName() => $newParentId, |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$where = array( |
233
|
1 |
|
$dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
234
|
|
|
); |
235
|
1 |
|
$dbAdapter->update($options->getTableName(), $bind, $where); |
236
|
1 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
2 |
|
public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
242
|
|
|
{ |
243
|
2 |
|
$options = $this->getOptions(); |
244
|
|
|
|
245
|
2 |
|
if (0 == $shift) { |
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
250
|
|
|
|
251
|
2 |
|
$sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
252
|
2 |
|
.' SET '.$dbAdapter->quoteIdentifier($options->getLevelColumnName()) |
253
|
2 |
|
.' = '.$dbAdapter->quoteIdentifier($options->getLevelColumnName()).' + :shift' |
254
|
2 |
|
.' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
255
|
2 |
|
.' >= :leftFrom'.' AND '.$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
256
|
2 |
|
.' <= :rightTo'; |
257
|
|
|
|
258
|
2 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
259
|
1 |
|
$sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$binds = array( |
263
|
2 |
|
':shift' => $shift, |
264
|
2 |
|
':leftFrom' => $leftIndexFrom, |
265
|
2 |
|
':rightTo' => $rightIndexTo, |
266
|
|
|
); |
267
|
|
|
|
268
|
2 |
|
$dbAdapter->prepare($sql)->execute($binds); |
269
|
2 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* {@inheritdoc} |
273
|
|
|
*/ |
274
|
2 |
|
public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
275
|
|
|
{ |
276
|
2 |
|
if (0 == $shift) { |
277
|
|
|
return; |
278
|
|
|
} |
279
|
|
|
|
280
|
2 |
|
$options = $this->getOptions(); |
281
|
|
|
|
282
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
283
|
|
|
|
284
|
2 |
|
$sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
285
|
2 |
|
.' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
286
|
2 |
|
.' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift, ' |
287
|
2 |
|
.$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
288
|
2 |
|
.' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift' |
289
|
2 |
|
.' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' >= :leftFrom' |
290
|
2 |
|
.' AND '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' <= :rightTo'; |
291
|
|
|
|
292
|
2 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
293
|
1 |
|
$sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$binds = array( |
297
|
2 |
|
':shift' => $shift, |
298
|
2 |
|
':leftFrom' => $leftIndexFrom, |
299
|
2 |
|
':rightTo' => $rightIndexTo, |
300
|
|
|
); |
301
|
|
|
|
302
|
2 |
|
$dbAdapter->prepare($sql)->execute($binds); |
303
|
2 |
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* {@inheritdoc} |
307
|
|
|
*/ |
308
|
4 |
|
public function getRoots($scope = null): array |
309
|
|
|
{ |
310
|
4 |
|
$options = $this->getOptions(); |
311
|
|
|
|
312
|
4 |
|
$dbAdapter = $this->getDbAdapter(); |
313
|
|
|
|
314
|
4 |
|
$select = $this->getBlankDbSelect() |
315
|
4 |
|
->where($options->getParentIdColumnName(true).' IS NULL') |
316
|
4 |
|
->order($options->getIdColumnName(true)); |
317
|
|
|
|
318
|
4 |
|
if (null != $scope && $options->getScopeColumnName()) { |
|
|
|
|
319
|
1 |
|
$select->where($options->getScopeColumnName(true).' = ?', $scope); |
320
|
|
|
} |
321
|
|
|
|
322
|
4 |
|
return $dbAdapter->fetchAll($select); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* {@inheritdoc} |
327
|
|
|
*/ |
328
|
2 |
|
public function getRoot($scope = null): array |
329
|
|
|
{ |
330
|
2 |
|
$result = $this->getRoots($scope); |
331
|
|
|
|
332
|
2 |
|
return ($result) ? $result[0] : array(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* {@inheritdoc} |
337
|
|
|
*/ |
338
|
2 |
|
public function getNode($nodeId): ?array |
339
|
|
|
{ |
340
|
2 |
|
$options = $this->getOptions(); |
341
|
|
|
|
342
|
2 |
|
$nodeId = (int) $nodeId; |
343
|
|
|
|
344
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
345
|
|
|
|
346
|
2 |
|
$select = $this->getDefaultDbSelect() |
347
|
2 |
|
->where($options->getIdColumnName(true).' = ?', $nodeId); |
348
|
|
|
|
349
|
2 |
|
$row = $dbAdapter->fetchRow($select); |
350
|
|
|
|
351
|
2 |
|
return $row ? $row : null; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* {@inheritdoc} |
356
|
|
|
*/ |
357
|
19 |
|
public function getNodeInfo($nodeId): ?NodeInfo |
358
|
|
|
{ |
359
|
19 |
|
$options = $this->getOptions(); |
360
|
|
|
|
361
|
19 |
|
$nodeId = (int) $nodeId; |
362
|
|
|
|
363
|
19 |
|
$dbAdapter = $this->getDbAdapter(); |
364
|
|
|
|
365
|
19 |
|
$select = $this->getBlankDbSelect() |
366
|
19 |
|
->where($options->getIdColumnName(true).' = ?', $nodeId); |
367
|
|
|
|
368
|
19 |
|
$row = $dbAdapter->fetchRow($select); |
369
|
|
|
|
370
|
19 |
|
$data = $row ? $row : null; |
371
|
|
|
|
372
|
19 |
|
$result = ($data) ? $this->_buildNodeInfoObject($data) : null; |
373
|
|
|
|
374
|
19 |
|
return $result; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* {@inheritdoc} |
379
|
|
|
*/ |
380
|
4 |
|
public function getChildrenNodeInfo($parentNodeId): array |
381
|
|
|
{ |
382
|
4 |
|
$dbAdapter = $this->getDbAdapter(); |
383
|
4 |
|
$options = $this->getOptions(); |
384
|
|
|
|
385
|
|
|
$columns = array( |
386
|
4 |
|
$options->getIdColumnName(), |
387
|
4 |
|
$options->getLeftColumnName(), |
388
|
4 |
|
$options->getRightColumnName(), |
389
|
4 |
|
$options->getParentIdColumnName(), |
390
|
4 |
|
$options->getLevelColumnName(), |
391
|
|
|
); |
392
|
|
|
|
393
|
4 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
394
|
2 |
|
$columns[] = $options->getScopeColumnName(); |
395
|
|
|
} |
396
|
|
|
|
397
|
4 |
|
$select = $this->getBlankDbSelect(); |
398
|
4 |
|
$select->reset(\Zend_Db_Select::COLUMNS); |
399
|
4 |
|
$select->columns($columns); |
400
|
4 |
|
$select->order($options->getLeftColumnName(true)); |
401
|
4 |
|
$select->where($options->getParentIdColumnName(true).' = ?', $parentNodeId); |
402
|
|
|
|
403
|
4 |
|
$data = $dbAdapter->fetchAll($select); |
404
|
|
|
|
405
|
4 |
|
$result = array(); |
406
|
|
|
|
407
|
4 |
|
foreach ($data as $nodeData) { |
408
|
3 |
|
$result[] = $this->_buildNodeInfoObject($nodeData); |
409
|
|
|
} |
410
|
|
|
|
411
|
4 |
|
return $result; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* {@inheritdoc} |
416
|
|
|
*/ |
417
|
2 |
|
public function updateNodeMetadata(NodeInfo $nodeInfo): void |
418
|
|
|
{ |
419
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
420
|
2 |
|
$options = $this->getOptions(); |
421
|
|
|
|
422
|
|
|
$bind = array( |
423
|
2 |
|
$options->getRightColumnName() => $nodeInfo->getRight(), |
424
|
2 |
|
$options->getLeftColumnName() => $nodeInfo->getLeft(), |
425
|
2 |
|
$options->getLevelColumnName() => $nodeInfo->getLevel(), |
426
|
|
|
); |
427
|
|
|
|
428
|
|
|
$where = array( |
429
|
2 |
|
$dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeInfo->getId(), |
430
|
|
|
); |
431
|
|
|
|
432
|
2 |
|
$dbAdapter->update($options->getTableName(), $bind, $where); |
433
|
2 |
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* {@inheritdoc} |
437
|
|
|
*/ |
438
|
6 |
|
public function getAncestors($nodeId, int $startLevel = 0, int $excludeLastNLevels = 0): array |
439
|
|
|
{ |
440
|
6 |
|
$options = $this->getOptions(); |
441
|
|
|
|
442
|
6 |
|
$startLevel = (int) $startLevel; |
443
|
|
|
|
444
|
|
|
// node does not exist |
445
|
6 |
|
if (!$nodeInfo = $this->getNodeInfo($nodeId)) { |
446
|
1 |
|
return array(); |
447
|
|
|
} |
448
|
|
|
|
449
|
5 |
|
$dbAdapter = $this->getDbAdapter(); |
450
|
|
|
|
451
|
5 |
|
$select = $this->getDefaultDbSelect(); |
452
|
|
|
|
453
|
5 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
454
|
2 |
|
$select->where($options->getScopeColumnName(true).' = ?', $nodeInfo->getScope()); |
455
|
|
|
} |
456
|
|
|
|
457
|
5 |
|
$select->where( |
458
|
5 |
|
$dbAdapter->quoteIdentifier($options->getLeftColumnName(true)).' <= ?', $nodeInfo->getLeft() |
459
|
5 |
|
)->where( |
460
|
5 |
|
$dbAdapter->quoteIdentifier($options->getRightColumnName(true)).' >= ?', $nodeInfo->getRight() |
461
|
5 |
|
)->order($options->getLeftColumnName(true).' ASC'); |
462
|
|
|
|
463
|
5 |
|
if (0 < $startLevel) { |
464
|
2 |
|
$select->where( |
465
|
2 |
|
$dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' >= ?', $startLevel |
466
|
|
|
); |
467
|
|
|
} |
468
|
|
|
|
469
|
5 |
|
if (0 < $excludeLastNLevels) { |
470
|
2 |
|
$select->where( |
471
|
2 |
|
$dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' <= ?', $nodeInfo->getLevel() - $excludeLastNLevels |
472
|
|
|
); |
473
|
|
|
} |
474
|
|
|
|
475
|
5 |
|
$result = $dbAdapter->fetchAll($select); |
476
|
|
|
|
477
|
5 |
|
return $result; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* {@inheritdoc} |
482
|
|
|
*/ |
483
|
9 |
|
public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, $excludeBranch = null): array |
484
|
|
|
{ |
485
|
9 |
|
$options = $this->getOptions(); |
486
|
|
|
|
487
|
9 |
|
if (!$nodeInfo = $this->getNodeInfo($nodeId)) { |
488
|
1 |
|
return array(); |
489
|
|
|
} |
490
|
|
|
|
491
|
8 |
|
$dbAdapter = $this->getDbAdapter(); |
492
|
8 |
|
$select = $this->getDefaultDbSelect(); |
493
|
8 |
|
$select->order($options->getLeftColumnName(true).' ASC'); |
494
|
|
|
|
495
|
8 |
|
if ($options->getScopeColumnName()) { |
|
|
|
|
496
|
3 |
|
$select->where($options->getScopeColumnName(true).' = ?', $nodeInfo->getScope()); |
497
|
|
|
} |
498
|
|
|
|
499
|
8 |
|
if (0 != $startLevel) { |
500
|
3 |
|
$level = $nodeInfo->getLevel() + (int) $startLevel; |
501
|
3 |
|
$select->where( |
502
|
3 |
|
$dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' >= ?', $level |
503
|
|
|
); |
504
|
|
|
} |
505
|
|
|
|
506
|
8 |
|
if (null != $levels) { |
|
|
|
|
507
|
2 |
|
$endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels); |
508
|
2 |
|
$select->where( |
509
|
2 |
|
$dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' < ?', $endLevel |
510
|
|
|
); |
511
|
|
|
} |
512
|
|
|
|
513
|
8 |
|
if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) { |
514
|
2 |
|
$where = sprintf( |
515
|
2 |
|
'(%s OR %s) AND (%s OR %s)', |
516
|
2 |
|
$this->getWhereBetween($options->getLeftColumnName(true), $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1), |
517
|
2 |
|
$this->getWhereBetween($options->getLeftColumnName(true), |
518
|
2 |
|
$excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()), |
519
|
2 |
|
$this->getWhereBetween($options->getRightColumnName(true), |
520
|
2 |
|
$excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()), |
521
|
2 |
|
$this->getWhereBetween($options->getRightColumnName(true), |
522
|
2 |
|
$nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1) |
523
|
|
|
); |
524
|
2 |
|
$select->where($where); |
525
|
|
|
} else { |
526
|
6 |
|
$select->where( |
527
|
6 |
|
$dbAdapter->quoteIdentifier($options->getLeftColumnName(true)).' >= ?', $nodeInfo->getLeft() |
528
|
|
|
); |
529
|
6 |
|
$select->where( |
530
|
6 |
|
$dbAdapter->quoteIdentifier($options->getRightColumnName(true)).' <= ?', $nodeInfo->getRight() |
531
|
|
|
); |
532
|
|
|
} |
533
|
|
|
|
534
|
8 |
|
$resultArray = $dbAdapter->fetchAll($select); |
535
|
|
|
|
536
|
8 |
|
return (0 < count($resultArray)) ? $resultArray : array(); |
537
|
|
|
} |
538
|
|
|
|
539
|
2 |
|
protected function getWhereBetween($column, $first, $second) |
540
|
|
|
{ |
541
|
2 |
|
$dbAdapter = $this->getDbAdapter(); |
542
|
2 |
|
$quotedColumn = $dbAdapter->quoteIdentifier($column); |
543
|
2 |
|
$quotedFirst = $dbAdapter->quote($first); |
544
|
2 |
|
$quotedSecond = $dbAdapter->quote($second); |
545
|
|
|
|
546
|
2 |
|
return sprintf('(%s BETWEEN %s AND %s)', $quotedColumn, $quotedFirst, $quotedSecond); |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: