@@ -53,7 +53,16 @@ class Node extends Eloquent {
5353 *
5454 * @since 1.1
5555 */
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 ;
5766
5867 /**
5968 * Pending operation.
@@ -83,7 +92,7 @@ protected static function boot()
8392 {
8493 parent ::boot ();
8594
86- static ::$ softDelete = static ::getIsSoftDelete ();
95+ static ::$ _softDelete = static ::getIsSoftDelete ();
8796
8897 static ::signOnEvents ();
8998 }
@@ -110,7 +119,7 @@ protected static function signOnEvents()
110119 return $ model ->callPendingAction ();
111120 });
112121
113- if ( ! static ::$ softDelete )
122+ if ( ! static ::$ _softDelete )
114123 {
115124 static ::deleting (function ($ model )
116125 {
@@ -139,6 +148,21 @@ public function save(array $options = array())
139148 });
140149 }
141150
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+
142166 /**
143167 * Set an action.
144168 *
@@ -648,18 +672,26 @@ protected function insertNode($position)
648672
649673 /**
650674 * Update the tree when the node is removed physically.
651- *
652- * @return void
653675 */
654676 protected function deleteNode ()
655677 {
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 );
658692
659693 // In case if user wants to re-create the node
660694 $ this ->makeRoot ();
661-
662- return $ this ->makeGap ($ this ->getRgt () + 1 , - $ this ->getNodeHeight ());
663695 }
664696
665697 /**
@@ -679,7 +711,7 @@ public function newEloquentBuilder($query)
679711 */
680712 protected function newServiceQuery ()
681713 {
682- return static ::$ softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
714+ return static ::$ _softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
683715 }
684716
685717 /**
0 commit comments