@@ -53,7 +53,16 @@ class Node extends Eloquent {
53
53
*
54
54
* @since 1.1
55
55
*/
56
- static protected $ softDelete ;
56
+ static protected $ _softDelete ;
57
+
58
+ /**
59
+ * Whether the node is being deleted.
60
+ *
61
+ * @since 2.0
62
+ *
63
+ * @var bool
64
+ */
65
+ static protected $ deleting ;
57
66
58
67
/**
59
68
* Pending operation.
@@ -83,7 +92,7 @@ protected static function boot()
83
92
{
84
93
parent ::boot ();
85
94
86
- static ::$ softDelete = static ::getIsSoftDelete ();
95
+ static ::$ _softDelete = static ::getIsSoftDelete ();
87
96
88
97
static ::signOnEvents ();
89
98
}
@@ -110,7 +119,7 @@ protected static function signOnEvents()
110
119
return $ model ->callPendingAction ();
111
120
});
112
121
113
- if ( ! static ::$ softDelete )
122
+ if ( ! static ::$ _softDelete )
114
123
{
115
124
static ::deleting (function ($ model )
116
125
{
@@ -139,6 +148,21 @@ public function save(array $options = array())
139
148
});
140
149
}
141
150
151
+ /**
152
+ * {@inheritdoc}
153
+ *
154
+ * Delete a node in transaction if model is not soft deleting.
155
+ */
156
+ public function delete ()
157
+ {
158
+ if (static ::$ _softDelete ) return parent ::delete ();
159
+
160
+ return $ this ->getConnection ()->transaction (function ()
161
+ {
162
+ return parent ::delete ();
163
+ });
164
+ }
165
+
142
166
/**
143
167
* Set an action.
144
168
*
@@ -648,18 +672,26 @@ protected function insertNode($position)
648
672
649
673
/**
650
674
* Update the tree when the node is removed physically.
651
- *
652
- * @return void
653
675
*/
654
676
protected function deleteNode ()
655
677
{
656
- // DBMS with support of foreign keys will remove descendant nodes automatically
657
- $ this ->newQuery ()->whereNodeBetween ([ $ this ->getLft (), $ this ->getRgt () ])->delete ();
678
+ if (static ::$ deleting ) return ;
679
+
680
+ $ lft = $ this ->getLft ();
681
+ $ rgt = $ this ->getRgt ();
682
+ $ height = $ rgt - $ lft + 1 ;
683
+
684
+ // Make sure that inner nodes are just deleted and don't touch the tree
685
+ static ::$ deleting = true ;
686
+
687
+ $ this ->newQuery ()->whereNodeBetween ([ $ lft , $ rgt ])->delete ();
688
+
689
+ static ::$ deleting = false ;
690
+
691
+ $ this ->makeGap ($ rgt + 1 , -$ height );
658
692
659
693
// In case if user wants to re-create the node
660
694
$ this ->makeRoot ();
661
-
662
- return $ this ->makeGap ($ this ->getRgt () + 1 , - $ this ->getNodeHeight ());
663
695
}
664
696
665
697
/**
@@ -679,7 +711,7 @@ public function newEloquentBuilder($query)
679
711
*/
680
712
protected function newServiceQuery ()
681
713
{
682
- return static ::$ softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
714
+ return static ::$ _softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
683
715
}
684
716
685
717
/**
0 commit comments