3
3
namespace Kalnoy \Nestedset ;
4
4
5
5
use Exception ;
6
+ use Illuminate \Database \Eloquent \Builder ;
6
7
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
7
8
use Illuminate \Database \Eloquent \Relations \HasMany ;
9
+ use Illuminate \Database \Eloquent \SoftDeletingScope ;
8
10
use LogicException ;
9
11
use Illuminate \Database \Eloquent \Model as Eloquent ;
10
12
use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
@@ -46,24 +48,6 @@ class Node extends Eloquent
46
48
*/
47
49
const AFTER = 'after ' ;
48
50
49
- /**
50
- * Whether model uses soft delete.
51
- *
52
- * @var bool
53
- *
54
- * @since 1.1
55
- */
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 ;
66
-
67
51
/**
68
52
* Pending operation.
69
53
*
@@ -97,23 +81,9 @@ protected static function boot()
97
81
{
98
82
parent ::boot ();
99
83
100
- static ::$ _softDelete = static ::getIsSoftDelete ();
101
-
102
84
static ::signOnEvents ();
103
85
}
104
86
105
- /**
106
- * Get whether model uses soft delete.
107
- *
108
- * @return bool
109
- */
110
- protected static function getIsSoftDelete ()
111
- {
112
- $ instance = new static ;
113
-
114
- return method_exists ($ instance , 'withTrashed ' );
115
- }
116
-
117
87
/**
118
88
* Sign on model events.
119
89
*/
@@ -132,7 +102,7 @@ protected static function signOnEvents()
132
102
$ model ->deleteDescendants ();
133
103
});
134
104
135
- if (static ::$ _softDelete ) {
105
+ if (static ::usesSoftDelete () ) {
136
106
static ::restoring (function (Node $ model ) {
137
107
static ::$ deletedAt = $ model ->{$ model ->getDeletedAtColumn ()};
138
108
});
@@ -146,7 +116,7 @@ protected static function signOnEvents()
146
116
/**
147
117
* {@inheritdoc}
148
118
*
149
- * Saves a node in a transaction.
119
+ * Saves a node in transaction.
150
120
*/
151
121
public function save (array $ options = array ())
152
122
{
@@ -158,7 +128,7 @@ public function save(array $options = array())
158
128
/**
159
129
* {@inheritdoc}
160
130
*
161
- * Delete a node in transaction if model is not soft deleting .
131
+ * Delete a node in transaction.
162
132
*/
163
133
public function delete ()
164
134
{
@@ -208,6 +178,22 @@ protected function callPendingAction()
208
178
$ this ->moved = call_user_func_array ([ $ this , $ method ], $ parameters );
209
179
}
210
180
181
+ /**
182
+ * @return bool
183
+ */
184
+ public static function usesSoftDelete ()
185
+ {
186
+ static $ softDelete ;
187
+
188
+ if (is_null ($ softDelete )) {
189
+ $ instance = new static ;
190
+
191
+ return $ softDelete = method_exists ($ instance , 'withTrashed ' );
192
+ }
193
+
194
+ return $ softDelete ;
195
+ }
196
+
211
197
/**
212
198
* Make a root node.
213
199
*/
@@ -685,24 +671,19 @@ protected function insertNode($position)
685
671
*/
686
672
protected function deleteDescendants ()
687
673
{
688
- if (static ::$ deleting ) return ;
689
-
690
674
$ lft = $ this ->getLft ();
691
675
$ rgt = $ this ->getRgt ();
692
676
693
- // Make sure that inner nodes are just deleted and don't touch the tree
694
- // This makes sense in Laravel 4.2
695
- static ::$ deleting = true ;
696
-
677
+ /** @var QueryBuilder $query */
697
678
$ query = $ this ->newQuery ()->whereNodeBetween ([ $ lft + 1 , $ rgt ]);
698
679
699
- if ( static :: $ _softDelete && $ this -> forceDeleting ) {
700
- $ query -> withTrashed ()-> forceDelete ();
701
- } else {
702
- $ query ->delete ( );
680
+ // Remove soft deleting scope when user forced delete so that descendants
681
+ // are also deleted physically
682
+ if ( $ this -> usesSoftDelete () && $ this -> forceDeleting ) {
683
+ $ query ->withoutGlobalScope (SoftDeletingScope::class );
703
684
}
704
685
705
- static :: $ deleting = false ;
686
+ $ query -> applyScopes ()-> delete () ;
706
687
707
688
if ($ this ->hardDeleting ()) {
708
689
$ height = $ rgt - $ lft + 1 ;
@@ -726,6 +707,7 @@ protected function restoreDescendants($deletedAt)
726
707
$ this ->newQuery ()
727
708
->whereNodeBetween ([ $ this ->getLft () + 1 , $ this ->getRgt () ])
728
709
->where ($ this ->getDeletedAtColumn (), '>= ' , $ deletedAt )
710
+ ->applyScopes ()
729
711
->restore ();
730
712
}
731
713
@@ -748,7 +730,7 @@ public function newEloquentBuilder($query)
748
730
*/
749
731
public function newServiceQuery ()
750
732
{
751
- return static :: $ _softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
733
+ return $ this -> usesSoftDelete () ? $ this ->withTrashed () : $ this ->newQuery ();
752
734
}
753
735
754
736
/**
@@ -779,9 +761,8 @@ public function newFromBuilder($attributes = array(), $connection = null)
779
761
*
780
762
* @param Node $parent
781
763
*/
782
- public static function create (array $ attributes = array (),
783
- Node $ parent = null
784
- ) {
764
+ public static function create (array $ attributes = [], Node $ parent = null )
765
+ {
785
766
$ children = array_pull ($ attributes , 'children ' );
786
767
787
768
$ instance = new static ($ attributes );
@@ -1146,7 +1127,7 @@ protected function getArrayableRelations()
1146
1127
*/
1147
1128
protected function hardDeleting ()
1148
1129
{
1149
- return ! static :: $ _softDelete or $ this ->forceDeleting ;
1130
+ return ! $ this -> usesSoftDelete () || $ this ->forceDeleting ;
1150
1131
}
1151
1132
1152
1133
/**
0 commit comments