diff --git a/src/NodeTrait.php b/src/NodeTrait.php index 0b985ab..1367fb1 100644 --- a/src/NodeTrait.php +++ b/src/NodeTrait.php @@ -49,10 +49,9 @@ public static function bootNodeTrait() static::deleting(function ($model) { // We will need fresh data to delete node safely + // We must delete the descendants BEFORE we delete the actual + // album to avoid failing FOREIGN key constraints. $model->refreshNode(); - }); - - static::deleted(function ($model) { $model->deleteDescendants(); }); @@ -249,7 +248,7 @@ public function children() */ public function descendants() { - return new DescendantsRelation($this->newQuery(), $this); + return new DescendantsRelation($this->newQueryWithoutScopes(), $this); } /** @@ -338,7 +337,7 @@ public function prevNodes() */ public function ancestors() { - return new AncestorsRelation($this->newQuery(), $this); + return new AncestorsRelation($this->newQueryWithoutScopes(), $this); } /** @@ -675,7 +674,7 @@ public function newNestedSetQuery($table = null) { $builder = $this->usesSoftDelete() ? $this->withTrashed() - : $this->newQuery(); + : $this->newQueryWithoutScopes(); return $this->applyNestedSetScope($builder, $table); } @@ -687,7 +686,7 @@ public function newNestedSetQuery($table = null) */ public function newScopedQuery($table = null) { - return $this->applyNestedSetScope($this->newQuery(), $table); + return $this->applyNestedSetScope($this->newQueryWithoutScopes(), $table); } /** diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 61aba6a..4920b58 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -31,18 +31,21 @@ class QueryBuilder extends Builder */ public function getNodeData($id, $required = false) { - $query = $this->toBase(); + $lftName = $this->model->getLftName(); + $rgtName = $this->model->getRgtName(); - $query->where($this->model->getKeyName(), '=', $id); - - $data = $query->first([ $this->model->getLftName(), - $this->model->getRgtName() ]); + $data = $this->toBase() + ->where($this->model->getKeyName(), '=', $id) + ->first([$lftName, $rgtName]); if ( ! $data && $required) { throw new ModelNotFoundException; } - - return (array)$data; + // Ensure that the result only contains the required attributes in + // correct order and nothing else. + // The query above might accidentally return more attributes, if + // a global scope is defined for the query by the base model. + return $data ? [$lftName => $data[$lftName], $rgtName => $data[$rgtName]] : []; } /**