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