From 93f9579cf097b945241154c2de7526c72ab72e9c Mon Sep 17 00:00:00 2001 From: Matthias Nagel Date: Sun, 30 May 2021 16:16:45 +0200 Subject: [PATCH 1/2] Fixes --- src/NodeTrait.php | 8 ++++---- src/QueryBuilder.php | 17 ++++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/NodeTrait.php b/src/NodeTrait.php index 0b985ab..71b8299 100644 --- a/src/NodeTrait.php +++ b/src/NodeTrait.php @@ -249,7 +249,7 @@ public function children() */ public function descendants() { - return new DescendantsRelation($this->newQuery(), $this); + return new DescendantsRelation($this->newQueryWithoutScopes(), $this); } /** @@ -338,7 +338,7 @@ public function prevNodes() */ public function ancestors() { - return new AncestorsRelation($this->newQuery(), $this); + return new AncestorsRelation($this->newQueryWithoutScopes(), $this); } /** @@ -675,7 +675,7 @@ public function newNestedSetQuery($table = null) { $builder = $this->usesSoftDelete() ? $this->withTrashed() - : $this->newQuery(); + : $this->newQueryWithoutScopes(); return $this->applyNestedSetScope($builder, $table); } @@ -687,7 +687,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]] : []; } /** From 736de7728389a25f4b22a28aa3636285e570361a Mon Sep 17 00:00:00 2001 From: Matthias Nagel Date: Thu, 6 Jan 2022 17:19:39 +0100 Subject: [PATCH 2/2] Fixed order of deletion. --- src/NodeTrait.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/NodeTrait.php b/src/NodeTrait.php index 71b8299..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(); });