1
- <?php namespace Kalnoy \Nestedset ;
1
+ <?php
2
+
3
+ namespace Kalnoy \Nestedset ;
2
4
3
5
use \Illuminate \Database \Eloquent \Model as Eloquent ;
4
6
use \Illuminate \Database \Query \Builder ;
@@ -41,6 +43,27 @@ class Node extends Eloquent {
41
43
*/
42
44
const AFTER = 'after ' ;
43
45
46
+ /**
47
+ * Whether model uses soft delete.
48
+ *
49
+ * @var bool
50
+ *
51
+ * @since 1.1
52
+ */
53
+ static protected $ softDelete ;
54
+
55
+ /**
56
+ * {@inheritdoc}
57
+ */
58
+ protected static function boot ()
59
+ {
60
+ parent ::boot ();
61
+
62
+ $ instance = new static ;
63
+
64
+ static ::$ softDelete = method_exists ($ instance , 'withTrashed ' );
65
+ }
66
+
44
67
/**
45
68
* Get the root node.
46
69
*
@@ -300,7 +323,7 @@ protected function beforeOrAfter(Node $node, $dir)
300
323
*/
301
324
protected function checkTarget (Node $ node )
302
325
{
303
- if (! $ node ->exists || $ node ->isDirty (static ::LFT ))
326
+ if ( ! $ node ->exists || $ node ->isDirty (static ::LFT ))
304
327
{
305
328
throw new Exception ("Target node is updated but not saved. " );
306
329
}
@@ -361,19 +384,19 @@ public function fireModelEvent($event, $halt = true)
361
384
{
362
385
if ($ this ->exists )
363
386
{
364
- if ($ this ->isDirty (static ::LFT ) && !$ this ->updateTree ())
387
+ if ($ this ->isDirty (static ::LFT ) && ! $ this ->updateTree ())
365
388
{
366
389
return false ;
367
390
}
368
391
}
369
392
else
370
393
{
371
- if (! isset ($ this ->attributes [static ::LFT ]))
394
+ if ( ! isset ($ this ->attributes [static ::LFT ]))
372
395
{
373
396
throw new Exception ("Cannot save node until it is inserted. " );
374
397
}
375
398
376
- if (! $ this ->updateTree ()) return false ;
399
+ if ( ! $ this ->updateTree ()) return false ;
377
400
}
378
401
}
379
402
@@ -382,7 +405,7 @@ public function fireModelEvent($event, $halt = true)
382
405
throw new Exception ("Cannot delete root node. " );
383
406
}
384
407
385
- if ($ event === 'deleted ' && !$ this -> softDelete ) $ this ->deleteNode ();
408
+ if ($ event === 'deleted ' && ! static :: $ softDelete ) $ this ->deleteNode ();
386
409
387
410
return parent ::fireModelEvent ($ event , $ halt );
388
411
}
@@ -591,6 +614,16 @@ protected function newBaseQueryBuilder()
591
614
return new QueryBuilder ($ conn , $ grammar , $ conn ->getPostProcessor (), $ this );
592
615
}
593
616
617
+ /**
618
+ * Get a new query including deleted nodes.
619
+ *
620
+ * @since 1.1
621
+ */
622
+ protected function newQueryWithDeleted ()
623
+ {
624
+ return static ::$ softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
625
+ }
626
+
594
627
/**
595
628
* Create a new NestedSet Collection instance.
596
629
*
@@ -610,7 +643,7 @@ public function newCollection(array $models = array())
610
643
*/
611
644
public function getNodeHeight ()
612
645
{
613
- if (! $ this ->exists ) return 2 ;
646
+ if ( ! $ this ->exists ) return 2 ;
614
647
615
648
return $ this ->attributes [static ::RGT ] - $ this ->attributes [static ::LFT ] + 1 ;
616
649
}
@@ -635,7 +668,7 @@ public function getDescendantCount()
635
668
*/
636
669
public function setParentIdAttribute ($ value )
637
670
{
638
- if (! isset ($ this ->attributes [static ::PARENT_ID ]) || $ this ->attributes [static ::PARENT_ID ] != $ value )
671
+ if ( ! isset ($ this ->attributes [static ::PARENT_ID ]) || $ this ->attributes [static ::PARENT_ID ] != $ value )
639
672
{
640
673
$ this ->appendTo (static ::findOrFail ($ value ));
641
674
}
0 commit comments