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