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