2
2
3
3
namespace Kalnoy \Nestedset ;
4
4
5
+ use Illuminate \Database \Eloquent \ModelNotFoundException ;
6
+ use Illuminate \Database \Query \Builder as Query ;
5
7
use LogicException ;
6
8
use Illuminate \Database \Eloquent \Builder ;
7
9
use Illuminate \Database \ConnectionInterface ;
11
13
12
14
class QueryBuilder extends Builder {
13
15
16
+ /**
17
+ * @var Node
18
+ */
19
+ protected $ model ;
20
+
14
21
/**
15
22
* Get node's `lft` and `rgt` values.
16
23
*
17
24
* @since 2.0
18
25
*
19
26
* @param mixed $id
27
+ * @param bool $required
20
28
*
21
29
* @return array
22
30
*/
23
- public function getNodeData ($ id )
31
+ public function getNodeData ($ id, $ required = false )
24
32
{
25
33
$ this ->query ->where ($ this ->model ->getKeyName (), '= ' , $ id );
26
34
27
- return (array )$ this ->query ->first ([ $ this ->model ->getLftName (), $ this ->model ->getRgtName () ]);
35
+ $ data = $ this ->query ->first ([ $ this ->model ->getLftName (), $ this ->model ->getRgtName () ]);
36
+
37
+ if ( ! $ data and $ required ) throw new ModelNotFoundException ;
38
+
39
+ return (array )$ data ;
28
40
}
29
41
30
42
/**
@@ -33,12 +45,13 @@ public function getNodeData($id)
33
45
* @since 2.0
34
46
*
35
47
* @param mixed $id
48
+ * @param bool $required
36
49
*
37
50
* @return array
38
51
*/
39
- public function getPlainNodeData ($ id )
52
+ public function getPlainNodeData ($ id, $ required = false )
40
53
{
41
- return array_values ($ this ->getNodeData ($ id ));
54
+ return array_values ($ this ->getNodeData ($ id, $ required ));
42
55
}
43
56
44
57
/**
@@ -144,7 +157,7 @@ public function orWhereNodeBetween($values)
144
157
*/
145
158
public function whereDescendantOf ($ id , $ boolean = 'and ' , $ not = false )
146
159
{
147
- $ data = $ this ->model ->newQuery ()->getPlainNodeData ($ id );
160
+ $ data = $ this ->model ->newServiceQuery ()->getPlainNodeData ($ id, true );
148
161
149
162
// Don't include the node
150
163
++$ data [0 ];
@@ -164,7 +177,15 @@ public function whereDescendantOf($id, $boolean = 'and', $not = false)
164
177
*/
165
178
public function descendantsOf ($ id , array $ columns = array ('* ' ))
166
179
{
167
- return $ this ->whereDescendantOf ($ id )->get ($ columns );
180
+ try
181
+ {
182
+ return $ this ->whereDescendantOf ($ id )->get ($ columns );
183
+ }
184
+
185
+ catch (ModelNotFoundException $ e )
186
+ {
187
+ return $ this ->model ->newCollection ();
188
+ }
168
189
}
169
190
170
191
/**
@@ -180,7 +201,7 @@ public function descendantsOf($id, array $columns = array('*'))
180
201
public function whereIsAfter ($ id , $ boolean = 'and ' )
181
202
{
182
203
$ table = $ this ->wrappedTable ();
183
- list ($ lft , $ rgt ) = $ this ->wrappedColumns ();
204
+ list ($ lft ,) = $ this ->wrappedColumns ();
184
205
$ key = $ this ->wrappedKey ();
185
206
186
207
$ this ->query ->whereRaw ("{$ lft } > (select _n. {$ lft } from {$ table } _n where _n. {$ key } = ?) " , [ $ id ], $ boolean );
@@ -201,7 +222,7 @@ public function whereIsAfter($id, $boolean = 'and')
201
222
public function whereIsBefore ($ id , $ boolean = 'and ' )
202
223
{
203
224
$ table = $ this ->wrappedTable ();
204
- list ($ lft , $ rgt ) = $ this ->wrappedColumns ();
225
+ list ($ lft ,) = $ this ->wrappedColumns ();
205
226
$ key = $ this ->wrappedKey ();
206
227
207
228
$ this ->query ->whereRaw ("{$ lft } < (select _b. {$ lft } from {$ table } _b where _b. {$ key } = ?) " , [ $ id ], $ boolean );
@@ -291,7 +312,7 @@ public function withoutRoot()
291
312
}
292
313
293
314
/**
294
- * Equivalent of `withouRoot `.
315
+ * Equivalent of `withoutRoot `.
295
316
*
296
317
* @since 2.0
297
318
*
@@ -358,7 +379,7 @@ public function reversed()
358
379
*/
359
380
public function moveNode ($ key , $ position )
360
381
{
361
- list ($ lft , $ rgt ) = $ this ->model ->newQuery ()->getPlainNodeData ($ key );
382
+ list ($ lft , $ rgt ) = $ this ->model ->newServiceQuery ()->getPlainNodeData ($ key, true );
362
383
363
384
if ($ lft < $ position && $ position < $ rgt )
364
385
{
@@ -384,7 +405,7 @@ public function moveNode($key, $position)
384
405
385
406
$ boundary = [ $ from , $ to ];
386
407
387
- $ query = $ this ->query ->where (function ($ inner ) use ($ boundary )
408
+ $ query = $ this ->query ->where (function (Query $ inner ) use ($ boundary )
388
409
{
389
410
$ inner ->whereBetween ($ this ->model ->getLftName (), $ boundary );
390
411
$ inner ->orWhereBetween ($ this ->model ->getRgtName (), $ boundary );
@@ -407,7 +428,7 @@ public function makeGap($cut, $height)
407
428
{
408
429
$ params = compact ('cut ' , 'height ' );
409
430
410
- $ this ->query ->where (function ($ inner ) use ($ cut )
431
+ $ this ->query ->where (function (Query $ inner ) use ($ cut )
411
432
{
412
433
$ inner ->where ($ this ->model ->getLftName (), '>= ' , $ cut );
413
434
$ inner ->orWhere ($ this ->model ->getRgtName (), '>= ' , $ cut );
@@ -453,13 +474,19 @@ protected function columnPatch($col, array $params)
453
474
{
454
475
extract ($ params );
455
476
477
+ /** @var int $height */
456
478
if ($ height > 0 ) $ height = '+ ' .$ height ;
457
479
458
480
if (isset ($ cut ))
459
481
{
460
482
return new Expression ("case when {$ col } >= {$ cut } then {$ col }{$ height } else {$ col } end " );
461
483
}
462
484
485
+ /** @var int $distance */
486
+ /** @var int $lft */
487
+ /** @var int $rgt */
488
+ /** @var int $from */
489
+ /** @var int $to */
463
490
if ($ distance > 0 ) $ distance = '+ ' .$ distance ;
464
491
465
492
return new Expression ("case " .
0 commit comments