Skip to content

Commit 44de773

Browse files
committed
Fix incompatibilities
1 parent 86bac38 commit 44de773

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/Kalnoy/Nestedset/Node.php

+42-10
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/Kalnoy/Nestedset/QueryBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getNodeData($id)
2424
{
2525
$this->query->where($this->model->getKeyName(), '=', $id);
2626

27-
return $this->query->first([ $this->model->getLftName(), $this->model->getRgtName() ]);
27+
return (array)$this->query->first([ $this->model->getLftName(), $this->model->getRgtName() ]);
2828
}
2929

3030
/**

tests/NodeTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,16 @@ public function testFailsToSaveNodeUntilNotInserted()
251251

252252
public function testNodeIsDeletedWithDescendants()
253253
{
254-
$node = $this->findCategory('notebooks');
254+
$node = $this->findCategory('mobile');
255255
$this->assertTrue($node->delete());
256256

257257
$this->assertTreeNotBroken();
258258

259-
$nodes = Category::whereIn('id', array(2, 3, 4))->count();
259+
$nodes = Category::whereIn('id', array(5, 6, 7, 8, 9))->count();
260260
$this->assertEquals(0, $nodes);
261+
262+
$root = Category::root();
263+
$this->assertEquals(8, $root->getRgt());
261264
}
262265

263266
/**

0 commit comments

Comments
 (0)