Skip to content

Commit

Permalink
Fix incompatibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
lazychaser committed Jun 17, 2014
1 parent 86bac38 commit 44de773
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
52 changes: 42 additions & 10 deletions src/Kalnoy/Nestedset/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ class Node extends Eloquent {
*
* @since 1.1
*/
static protected $softDelete;
static protected $_softDelete;

/**
* Whether the node is being deleted.
*
* @since 2.0
*
* @var bool
*/
static protected $deleting;

/**
* Pending operation.
Expand Down Expand Up @@ -83,7 +92,7 @@ protected static function boot()
{
parent::boot();

static::$softDelete = static::getIsSoftDelete();
static::$_softDelete = static::getIsSoftDelete();

static::signOnEvents();
}
Expand All @@ -110,7 +119,7 @@ protected static function signOnEvents()
return $model->callPendingAction();
});

if ( ! static::$softDelete)
if ( ! static::$_softDelete)
{
static::deleting(function ($model)
{
Expand Down Expand Up @@ -139,6 +148,21 @@ public function save(array $options = array())
});
}

/**
* {@inheritdoc}
*
* Delete a node in transaction if model is not soft deleting.
*/
public function delete()
{
if (static::$_softDelete) return parent::delete();

return $this->getConnection()->transaction(function ()
{
return parent::delete();
});
}

/**
* Set an action.
*
Expand Down Expand Up @@ -648,18 +672,26 @@ protected function insertNode($position)

/**
* Update the tree when the node is removed physically.
*
* @return void
*/
protected function deleteNode()
{
// DBMS with support of foreign keys will remove descendant nodes automatically
$this->newQuery()->whereNodeBetween([ $this->getLft(), $this->getRgt() ])->delete();
if (static::$deleting) return;

$lft = $this->getLft();
$rgt = $this->getRgt();
$height = $rgt - $lft + 1;

// Make sure that inner nodes are just deleted and don't touch the tree
static::$deleting = true;

$this->newQuery()->whereNodeBetween([ $lft, $rgt ])->delete();

static::$deleting = false;

$this->makeGap($rgt + 1, -$height);

// In case if user wants to re-create the node
$this->makeRoot();

return $this->makeGap($this->getRgt() + 1, - $this->getNodeHeight());
}

/**
Expand All @@ -679,7 +711,7 @@ public function newEloquentBuilder($query)
*/
protected function newServiceQuery()
{
return static::$softDelete ? $this->withTrashed() : $this->newQuery();
return static::$_softDelete ? $this->withTrashed() : $this->newQuery();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Kalnoy/Nestedset/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getNodeData($id)
{
$this->query->where($this->model->getKeyName(), '=', $id);

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

/**
Expand Down
7 changes: 5 additions & 2 deletions tests/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,16 @@ public function testFailsToSaveNodeUntilNotInserted()

public function testNodeIsDeletedWithDescendants()
{
$node = $this->findCategory('notebooks');
$node = $this->findCategory('mobile');
$this->assertTrue($node->delete());

$this->assertTreeNotBroken();

$nodes = Category::whereIn('id', array(2, 3, 4))->count();
$nodes = Category::whereIn('id', array(5, 6, 7, 8, 9))->count();
$this->assertEquals(0, $nodes);

$root = Category::root();
$this->assertEquals(8, $root->getRgt());
}

/**
Expand Down

0 comments on commit 44de773

Please sign in to comment.