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