forked from propelorm/Propel2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNestedSetBehaviorQueryBuilderModifier.php
More file actions
1122 lines (1028 loc) · 32.6 KB
/
NestedSetBehaviorQueryBuilderModifier.php
File metadata and controls
1122 lines (1028 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* MIT License. This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Propel\Generator\Behavior\NestedSet;
use Propel\Generator\Builder\Om\QueryBuilder;
use Propel\Generator\Model\Column;
/**
* Behavior to adds nested set tree structure columns and abilities
*
* @author François Zaninotto
*/
class NestedSetBehaviorQueryBuilderModifier
{
/**
* @var \Propel\Generator\Behavior\NestedSet\NestedSetBehavior
*/
protected $behavior;
/**
* @var \Propel\Generator\Model\Table
*/
protected $table;
/**
* @var \Propel\Generator\Builder\Om\QueryBuilder
*/
protected $builder;
/**
* @var string
*/
protected $objectClassName;
/**
* @var string
*/
protected $queryClassName;
/**
* @var string
*/
protected $tableMapClassName;
/**
* @param \Propel\Generator\Behavior\NestedSet\NestedSetBehavior $behavior
*/
public function __construct(NestedSetBehavior $behavior)
{
$this->behavior = $behavior;
$this->table = $behavior->getTable();
}
/**
* @param string $key
*
* @return mixed
*/
protected function getParameter(string $key)
{
return $this->behavior->getParameter($key);
}
/**
* @param string $name
*
* @return \Propel\Generator\Model\Column
*/
protected function getColumn(string $name): Column
{
return $this->behavior->getColumnForParameter($name);
}
/**
* @param \Propel\Generator\Builder\Om\QueryBuilder $builder
*
* @return void
*/
protected function setBuilder(QueryBuilder $builder): void
{
$this->builder = $builder;
$this->objectClassName = $builder->getObjectClassName();
$this->queryClassName = $builder->getQueryClassName();
$this->tableMapClassName = $builder->getTableMapClassName();
}
/**
* @param \Propel\Generator\Builder\Om\QueryBuilder $builder
*
* @return string
*/
public function queryMethods(QueryBuilder $builder): string
{
$this->setBuilder($builder);
$script = '';
// select filters
if ($this->behavior->useScope()) {
$this->addTreeRoots($script);
$this->addInTree($script);
}
$this->addDescendantsOf($script);
$this->addBranchOf($script);
$this->addChildrenOf($script);
$this->addSiblingsOf($script);
$this->addAncestorsOf($script);
$this->addRootsOf($script);
// select orders
$this->addOrderByBranch($script);
$this->addOrderByLevel($script);
// select termination methods
$this->addFindRoot($script);
if ($this->behavior->useScope()) {
$this->addFindRoots($script);
}
$this->addFindTree($script);
if ($this->behavior->useScope()) {
$this->addRetrieveRoots($script);
}
$this->addRetrieveRoot($script);
$this->addRetrieveTree($script);
$this->addIsValid($script);
$this->addDeleteTree($script);
$this->addShiftRLValues($script);
$this->addShiftLevel($script);
$this->addUpdateLoadedNodes($script);
$this->addMakeRoomForLeaf($script);
$this->addFixLevels($script);
if ($this->behavior->useScope()) {
$this->addSetNegativeScope($script);
}
return $script;
}
/**
* @param string $script
*
* @return void
*/
protected function addTreeRoots(string &$script): void
{
$leftColumnName = $this->behavior->getColumnConstant('left_column');
$script .= "
/**
* Filter the query to restrict the result to root objects
*
* @return \$this
*/
public function treeRoots()
{
\$this->addUsingOperator(\$this->resolveLocalColumnByName('{$leftColumnName}'), 1, Criteria::EQUAL);
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addInTree(string &$script): void
{
$scopeColumnName = $this->behavior->getColumnConstant('scope_column');
$script .= "
/**
* Returns the objects in a certain tree, from the tree scope
*
* @param int|null \$scope Scope to determine which objects node to return
*
* @return \$this
*/
public function inTree(?int \$scope = null)
{
\$this->addUsingOperator(\$this->resolveLocalColumnByName('{$scopeColumnName}'), \$scope, Criteria::EQUAL);
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addDescendantsOf(string &$script): void
{
$objectName = '$' . $this->table->getCamelCaseName();
$leftColumnName = $this->behavior->getColumnConstant('left_column');
$script .= "
/**
* Filter the query to restrict the result to descendants of an object
*
* @param {$this->objectClassName} $objectName The object to use for descendant search
*
* @return \$this
*/
public function descendantsOf($this->objectClassName $objectName)
{
\$leftColumn = \$this->resolveLocalColumnByName('{$leftColumnName}');
\$this";
if ($this->behavior->useScope()) {
$script .= "
->inTree({$objectName}->getScopeValue())";
}
$script .= "
->addUsingOperator(\$leftColumn, {$objectName}->getLeftValue(), Criteria::GREATER_THAN)
->addUsingOperator(\$leftColumn, {$objectName}->getRightValue(), Criteria::LESS_THAN);
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addBranchOf(string &$script): void
{
$objectName = '$' . $this->table->getCamelCaseName();
$leftColumnName = $this->behavior->getColumnConstant('left_column');
$script .= "
/**
* Filter the query to restrict the result to the branch of an object.
* Same as descendantsOf(), except that it includes the object passed as parameter in the result
*
* @param {$this->objectClassName} $objectName The object to use for branch search
*
* @return \$this
*/
public function branchOf($this->objectClassName $objectName)
{
\$leftColumn = \$this->resolveLocalColumnByName('{$leftColumnName}');
\$this";
if ($this->behavior->useScope()) {
$script .= "
->inTree({$objectName}->getScopeValue())";
}
$script .= "
->addUsingOperator(\$leftColumn, {$objectName}->getLeftValue(), Criteria::GREATER_EQUAL)
->addUsingOperator(\$leftColumn, {$objectName}->getRightValue(), Criteria::LESS_EQUAL);
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addChildrenOf(string &$script): void
{
$objectName = '$' . $this->table->getCamelCaseName();
$levelColumnName = $this->behavior->getColumnConstant('level_column');
$script .= "
/**
* Filter the query to restrict the result to children of an object
*
* @param {$this->objectClassName} $objectName The object to use for child search
*
* @return \$this
*/
public function childrenOf($this->objectClassName $objectName)
{
\$this
->descendantsOf($objectName)
->addUsingOperator(\$this->resolveLocalColumnByName('{$levelColumnName}'), {$objectName}->getLevel() + 1, Criteria::EQUAL);
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addSiblingsOf(string &$script): void
{
$objectName = '$' . $this->table->getCamelCaseName();
$script .= "
/**
* Filter the query to restrict the result to siblings of an object.
* The result does not include the object passed as parameter.
*
* @param {$this->objectClassName} $objectName The object to use for sibling search
* @param ConnectionInterface \$con Connection to use.
*
* @return \$this
*/
public function siblingsOf($this->objectClassName $objectName, ?ConnectionInterface \$con = null)
{
if ({$objectName}->isRoot()) {
\$this->
add({$this->objectClassName}::LEVEL_COL, '1<>1', Criteria::CUSTOM);
} else {
\$this
->childrenOf({$objectName}->getParent(\$con))
->prune($objectName);
}
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addAncestorsOf(string &$script): void
{
$objectName = '$' . $this->table->getCamelCaseName();
$leftColumnName = $this->behavior->getColumnConstant('left_column');
$rightColumnName = $this->behavior->getColumnConstant('right_column');
$script .= "
/**
* Filter the query to restrict the result to ancestors of an object
*
* @param {$this->objectClassName} $objectName The object to use for ancestors search
*
* @return \$this
*/
public function ancestorsOf($this->objectClassName $objectName)
{
\$this";
if ($this->behavior->useScope()) {
$script .= "
->inTree({$objectName}->getScopeValue())";
}
$script .= "
->addUsingOperator(\$this->resolveLocalColumnByName('{$leftColumnName}'), {$objectName}->getLeftValue(), Criteria::LESS_THAN)
->addUsingOperator(\$this->resolveLocalColumnByName('{$rightColumnName}'), {$objectName}->getRightValue(), Criteria::GREATER_THAN);
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addRootsOf(string &$script): void
{
$objectName = '$' . $this->table->getCamelCaseName();
$leftColumnName = $this->behavior->getColumnConstant('left_column');
$rightColumnName = $this->behavior->getColumnConstant('right_column');
$script .= "
/**
* Filter the query to restrict the result to roots of an object.
* Same as ancestorsOf(), except that it includes the object passed as parameter in the result
*
* @param {$this->objectClassName} $objectName The object to use for roots search
*
* @return \$this
*/
public function rootsOf($this->objectClassName $objectName)
{
\$this";
if ($this->behavior->useScope()) {
$script .= "
->inTree({$objectName}->getScopeValue())";
}
$script .= "
->addUsingOperator(\$this->resolveLocalColumnByName('{$leftColumnName}'), {$objectName}->getLeftValue(), Criteria::LESS_EQUAL)
->addUsingOperator(\$this->resolveLocalColumnByName('{$rightColumnName}'), {$objectName}->getRightValue(), Criteria::GREATER_EQUAL);
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addOrderByBranch(string &$script): void
{
$script .= "
/**
* Order the result by branch, i.e. natural tree order
*
* @param bool \$reverse if true, reverses the order
*
* @return \$this
*/
public function orderByBranch(\$reverse = false)
{
if (\$reverse) {
\$this
->addDescendingOrderByColumn({$this->objectClassName}::LEFT_COL);
} else {
\$this
->addAscendingOrderByColumn({$this->objectClassName}::LEFT_COL);
}
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addOrderByLevel(string &$script): void
{
$script .= "
/**
* Order the result by level, the closer to the root first
*
* @param bool \$reverse if true, reverses the order
*
* @return \$this
*/
public function orderByLevel(\$reverse = false)
{
if (\$reverse) {
\$this
->addDescendingOrderByColumn({$this->objectClassName}::LEVEL_COL)
->addDescendingOrderByColumn({$this->objectClassName}::LEFT_COL);
} else {
\$this
->addAscendingOrderByColumn({$this->objectClassName}::LEVEL_COL)
->addAscendingOrderByColumn({$this->objectClassName}::LEFT_COL);
}
return \$this;
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addFindRoot(string &$script): void
{
$useScope = $this->behavior->useScope();
$leftColumnName = $this->behavior->getColumnConstant('left_column');
$script .= "
/**
* Returns " . ($useScope ? 'a' : 'the') . " root node for the tree
*";
if ($useScope) {
$script .= "
* @param int \$scope Scope to determine which root node to return";
}
$script .= "
* @param ConnectionInterface \$con Connection to use.
*
* @return {$this->objectClassName} The tree root object
*/
public function findRoot(" . ($useScope ? '$scope = null, ' : '') . "?ConnectionInterface \$con = null)
{
return \$this
->addUsingOperator(\$this->resolveLocalColumnByName('{$leftColumnName}'), 1, Criteria::EQUAL)";
if ($useScope) {
$script .= "
->inTree(\$scope)";
}
$script .= "
->findOne(\$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addFindRoots(string &$script): void
{
$script .= "
/**
* Returns the root objects for all trees.
*
* @param ConnectionInterface \$con Connection to use.
*
* @return {$this->objectClassName}[]|ObjectCollection|mixed the list of results, formatted by the current formatter
*/
public function findRoots(?ConnectionInterface \$con = null)
{
return \$this
->treeRoots()
->find(\$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addFindTree(string &$script): void
{
$useScope = $this->behavior->useScope();
$script .= "
/**
* Returns " . ($useScope ? 'a' : 'the') . " tree of objects
*";
if ($useScope) {
$script .= "
* @param int \$scope Scope to determine which tree node to return";
}
$script .= "
* @param ConnectionInterface \$con Connection to use.
*
* @return {$this->objectClassName}[]|ObjectCollection|mixed the list of results, formatted by the current formatter
*/
public function findTree(" . ($useScope ? '$scope = null, ' : '') . "?ConnectionInterface \$con = null)
{
return \$this";
if ($useScope) {
$script .= "
->inTree(\$scope)";
}
$script .= "
->orderByBranch()
->find(\$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addRetrieveRoots(string &$script): void
{
$queryClassName = $this->queryClassName;
$objectClassName = $this->objectClassName;
$tableMapClassName = $this->builder->getTableMapClass();
$script .= "
/**
* Returns the root nodes for the tree
*
* @param Criteria \$criteria Optional Criteria to filter the query
* @param ConnectionInterface \$con Connection to use.
* @return {$this->objectClassName}[]|ObjectCollection|mixed the list of results, formatted by the current formatter
*/
static public function retrieveRoots(?Criteria \$criteria = null, ?ConnectionInterface \$con = null)
{
if (null === \$criteria) {
\$criteria = new Criteria($tableMapClassName::DATABASE_NAME);
}
\$criteria->addFilter($objectClassName::LEFT_COL, 1, Criteria::EQUAL);
return $queryClassName::create(null, \$criteria)->find(\$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addRetrieveRoot(string &$script): void
{
$queryClassName = $this->queryClassName;
$objectClassName = $this->objectClassName;
$useScope = $this->behavior->useScope();
$tableMapClassName = $this->builder->getTableMapClass();
$script .= "
/**
* Returns the root node for a given scope
*";
if ($useScope) {
$script .= "
* @param int \$scope Scope to determine which root node to return";
}
$script .= "
* @param ConnectionInterface \$con Connection to use.
* @return {$this->objectClassName} Propel object for root node
*/
static public function retrieveRoot(" . ($useScope ? '$scope = null, ' : '') . "?ConnectionInterface \$con = null)
{
\$c = new Criteria($tableMapClassName::DATABASE_NAME);
\$c->addFilter($objectClassName::LEFT_COL, 1, Criteria::EQUAL);";
if ($useScope) {
$script .= "
\$c->addFilter($objectClassName::SCOPE_COL, \$scope, Criteria::EQUAL);";
}
$script .= "
return $queryClassName::create(null, \$c)->findOne(\$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addRetrieveTree(string &$script): void
{
$queryClassName = $this->queryClassName;
$objectClassName = $this->objectClassName;
$useScope = $this->behavior->useScope();
$tableMapClassName = $this->builder->getTableMapClass();
$script .= "
/**
* Returns the whole tree node for a given scope
*";
if ($useScope) {
$script .= "
* @param int \$scope Scope to determine which root node to return";
}
$script .= "
* @param Criteria \$criteria Optional Criteria to filter the query
* @param ConnectionInterface \$con Connection to use.
* @return {$this->objectClassName}[]|ObjectCollection|mixed the list of results, formatted by the current formatter
*/
static public function retrieveTree(" . ($useScope ? '$scope = null, ' : '') . "?Criteria \$criteria = null, ?ConnectionInterface \$con = null)
{
if (null === \$criteria) {
\$criteria = new Criteria($tableMapClassName::DATABASE_NAME);
}
\$criteria->addAscendingOrderByColumn($objectClassName::LEFT_COL);";
if ($useScope) {
$script .= "
\$criteria->addFilter($objectClassName::SCOPE_COL, \$scope, Criteria::EQUAL);";
}
$script .= "
return $queryClassName::create(null, \$criteria)->find(\$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addIsValid(string &$script): void
{
$objectClassName = $this->objectClassName;
$script .= "
/**
* Tests if node is valid
*
* @param $objectClassName \$node Propel object for src node
* @return bool
*/
static public function isValid(?$objectClassName \$node = null)
{
if (is_object(\$node) && \$node->getRightValue() > \$node->getLeftValue()) {
return true;
} else {
return false;
}
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addDeleteTree(string &$script): void
{
$objectClassName = $this->objectClassName;
$useScope = $this->behavior->useScope();
$tableMapClassName = $this->builder->getTableMapClass();
$script .= "
/**
* Delete an entire tree
* ";
if ($useScope) {
$script .= "
* @param int \$scope Scope to determine which tree to delete";
}
$script .= "
* @param ConnectionInterface \$con Connection to use.
*
* @return int The number of deleted nodes
*/
static public function deleteTree(" . ($useScope ? '$scope = null, ' : '') . "?ConnectionInterface \$con = null)
{";
if ($useScope) {
$script .= "
\$c = new Criteria($tableMapClassName::DATABASE_NAME);
\$c->addFilter($objectClassName::SCOPE_COL, \$scope, Criteria::EQUAL);
return $tableMapClassName::doDelete(\$c, \$con);";
} else {
$script .= "
return $tableMapClassName::doDeleteAll(\$con);";
}
$script .= "
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addShiftRLValues(string &$script): void
{
$objectClassName = $this->objectClassName;
$useScope = $this->behavior->useScope();
$tableMapClassName = $this->builder->getTableMapClass();
$this->builder->declareClass('Propel\\Runtime\Map\\TableMap');
$script .= "
/**
* Adds \$delta to all L and R values that are >= \$first and <= \$last.
* '\$delta' can also be negative.
*
* @param int \$delta Value to be shifted by, can be negative
* @param int \$first First node to be shifted
* @param int \$last Last node to be shifted (optional)";
if ($useScope) {
$script .= "
* @param int \$scope Scope to use for the shift";
}
$script .= "
* @param ConnectionInterface \$con Connection to use.
*/
static public function shiftRLValues(\$delta, \$first, \$last = null" . ($useScope ? ', $scope = null' : '') . ", ?ConnectionInterface \$con = null)
{
if (\$con === null) {
\$con = Propel::getServiceContainer()->getWriteConnection($tableMapClassName::DATABASE_NAME);
}
// Shift left column values
\$updateQuery = new Criteria($tableMapClassName::DATABASE_NAME);
\$criterion = \$updateQuery->getNewCriterion($objectClassName::LEFT_COL, \$first, Criteria::GREATER_EQUAL);
if (null !== \$last) {
\$criterion->addAnd(\$updateQuery->getNewCriterion($objectClassName::LEFT_COL, \$last, Criteria::LESS_EQUAL));
}
\$updateQuery->addFilter(\$criterion);";
if ($useScope) {
$script .= "
\$updateQuery->addFilter($objectClassName::SCOPE_COL, \$scope, Criteria::EQUAL);";
}
$script .= "
\$updateQuery->setUpdateExpression($objectClassName::LEFT_COL, $objectClassName::LEFT_COL . ' + ?', \$delta, \PDO::PARAM_INT);
\$updateQuery->doUpdate(null, \$con);
// Shift right column values
\$updateQuery = new Criteria($tableMapClassName::DATABASE_NAME);
\$criterion = \$updateQuery->getNewCriterion($objectClassName::RIGHT_COL, \$first, Criteria::GREATER_EQUAL);
if (null !== \$last) {
\$criterion->addAnd(\$updateQuery->getNewCriterion($objectClassName::RIGHT_COL, \$last, Criteria::LESS_EQUAL));
}
\$updateQuery->addFilter(\$criterion);";
if ($useScope) {
$script .= "
\$updateQuery->addFilter($objectClassName::SCOPE_COL, \$scope, Criteria::EQUAL);";
}
$script .= "
\$updateQuery->setUpdateExpression($objectClassName::RIGHT_COL, $objectClassName::RIGHT_COL . ' + ?', \$delta, \PDO::PARAM_STR);
\$updateQuery->doUpdate(null, \$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addShiftLevel(string &$script): void
{
$objectClassName = $this->objectClassName;
$useScope = $this->behavior->useScope();
$tableMapClassName = $this->builder->getTableMapClass();
$this->builder->declareClass('Propel\\Runtime\Map\\TableMap');
$script .= "
/**
* Adds \$delta to level for nodes having left value >= \$first and right value <= \$last.
* '\$delta' can also be negative.
*
* @param int \$delta Value to be shifted by, can be negative
* @param int \$first First node to be shifted
* @param int \$last Last node to be shifted";
if ($useScope) {
$script .= "
* @param int \$scope Scope to use for the shift";
}
$script .= "
* @param ConnectionInterface \$con Connection to use.
*/
static public function shiftLevel(\$delta, \$first, \$last" . ($useScope ? ', $scope = null' : '') . ", ?ConnectionInterface \$con = null)
{
if (\$con === null) {
\$con = Propel::getServiceContainer()->getWriteConnection($tableMapClassName::DATABASE_NAME);
}
\$updateQuery = new Criteria($tableMapClassName::DATABASE_NAME);
\$updateQuery->addFilter($objectClassName::LEFT_COL, \$first, Criteria::GREATER_EQUAL);
\$updateQuery->addFilter($objectClassName::RIGHT_COL, \$last, Criteria::LESS_EQUAL);";
if ($useScope) {
$script .= "
\$updateQuery->addFilter($objectClassName::SCOPE_COL, \$scope, Criteria::EQUAL);";
}
$script .= "
\$updateQuery->setUpdateExpression($objectClassName::LEVEL_COL, $objectClassName::LEVEL_COL . ' + ?', \$delta, \PDO::PARAM_INT);
\$updateQuery->doUpdate(null, \$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addUpdateLoadedNodes(string &$script): void
{
$queryClassName = $this->queryClassName;
$objectClassName = $this->objectClassName;
$tableMapClassName = $this->tableMapClassName;
$script .= "
/**
* Reload all already loaded nodes to sync them with updated db
*
* @param $objectClassName \$prune Object to prune from the update
* @param ConnectionInterface \$con Connection to use.
*/
static public function updateLoadedNodes(\$prune = null, ?ConnectionInterface \$con = null)
{
if (Propel::isInstancePoolingEnabled()) {
\$keys = [];
/** @var \$obj $objectClassName */
foreach ($tableMapClassName::\$instances as \$obj) {
if (!\$prune || !\$prune->equals(\$obj)) {
\$keys[] = \$obj->getPrimaryKey();
}
}
if (!empty(\$keys)) {
// We don't need to alter the object instance pool; we're just modifying these ones
// already in the pool.
\$criteria = new Criteria($tableMapClassName::DATABASE_NAME);";
if (count($this->table->getPrimaryKey()) === 1) {
$pkey = $this->table->getPrimaryKey();
$col = array_shift($pkey);
$script .= "
\$criteria->addFilter(" . $this->builder->getColumnConstant($col) . ', $keys, Criteria::IN);';
} else {
$fields = [];
foreach ($this->table->getPrimaryKey() as $col) {
$fields[] = $this->builder->getColumnConstant($col);
}
$script .= "
// Loop on each instances in pool
foreach (\$keys as \$values) {
// Create initial Criterion
\$cton = \$criteria->getNewCriterion(" . $fields[0] . ', $values[0]);';
unset($fields[0]);
foreach ($fields as $k => $col) {
$script .= "
// Create next criterion
\$nextcton = \$criteria->getNewCriterion(" . $col . ", \$values[$k]);
// And merge it with the first
\$cton->addAnd(\$nextcton);";
}
$script .= "
// Add final Criterion to Criteria
\$criteria->addOr(\$cton);
}";
}
$script .= "
\$dataFetcher = $queryClassName::create(null, \$criteria)->fetch(\$con);
while (\$row = \$dataFetcher->fetch()) {
\$key = $tableMapClassName::getPrimaryKeyHashFromRow(\$row, 0);
/** @var \$object $objectClassName */
if (null !== (\$object = $tableMapClassName::getInstanceFromPool(\$key))) {";
$n = 0;
foreach ($this->table->getColumns() as $col) {
if ($col->isLazyLoad()) {
continue;
}
if ($col->getPhpName() === $this->getColumnPhpName('left_column')) {
$script .= "
\$object->setLeftValue(\$row[$n]);";
} elseif ($col->getPhpName() === $this->getColumnPhpName('right_column')) {
$script .= "
\$object->setRightValue(\$row[$n]);";
} elseif ($this->getParameter('use_scope') == 'true' && $col->getPhpName() === $this->getColumnPhpName('scope_column')) {
$script .= "
\$object->setScopeValue(\$row[$n]);";
} elseif ($col->getPhpName() === $this->getColumnPhpName('level_column')) {
$script .= "
\$object->setLevel(\$row[$n]);
\$object->clearNestedSetChildren();";
}
$n++;
}
$script .= "
}
}
\$dataFetcher->close();
}
}
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addMakeRoomForLeaf(string &$script): void
{
$queryClassName = $this->queryClassName;
$useScope = $this->behavior->useScope();
$script .= "
/**
* Update the tree to allow insertion of a leaf at the specified position
*
* @param int \$left left column value";
if ($useScope) {
$script .= "
* @param int \$scope scope column value";
}
$script .= "
* @param mixed \$prune Object to prune from the shift
* @param ConnectionInterface|null \$con Connection to use.
*/
static public function makeRoomForLeaf(\$left" . ($useScope ? ', $scope' : '') . ", \$prune = null, ?ConnectionInterface \$con = null)
{
// Update database nodes
$queryClassName::shiftRLValues(2, \$left, null" . ($useScope ? ', $scope' : '') . ", \$con);
// Update all loaded nodes
$queryClassName::updateLoadedNodes(\$prune, \$con);
}
";
}
/**
* @param string $script
*
* @return void
*/
protected function addFixLevels(string &$script): void
{
$objectClassName = $this->objectClassName;
$queryClassName = $this->queryClassName;
$tableMapClassName = $this->tableMapClassName;