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