From 536432a20eea297ee8fc48d6d5cd97f78b3e8af3 Mon Sep 17 00:00:00 2001 From: Djuuu Date: Mon, 13 Sep 2021 16:50:20 +0200 Subject: [PATCH] Column disambiguation in closure table queries --- src/Models/Entity.php | 12 ++++++------ .../2014_01_18_162506_create_entities_table.php | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Models/Entity.php b/src/Models/Entity.php index b5d04de..c255818 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -430,12 +430,12 @@ private function buildAncestorsQuery(Builder $builder, $id, $withSelf) return $builder ->join( $this->closure->getTable(), - $this->closure->getAncestorColumn(), + $this->closure->getQualifiedAncestorColumn(), '=', $this->getQualifiedKeyName() ) - ->where($this->closure->getDescendantColumn(), '=', $id) - ->where($this->closure->getDepthColumn(), $depthOperator, 0); + ->where($this->closure->getQualifiedDescendantColumn(), '=', $id) + ->where($this->closure->getQualifiedDepthColumn(), $depthOperator, 0); } /** @@ -563,12 +563,12 @@ private function buildDescendantsQuery(Builder $builder, $id, $withSelf) return $builder ->join( $this->closure->getTable(), - $this->closure->getDescendantColumn(), + $this->closure->getQualifiedDescendantColumn(), '=', $this->getQualifiedKeyName() ) - ->where($this->closure->getAncestorColumn(), '=', $id) - ->where($this->closure->getDepthColumn(), $depthOperator, 0); + ->where($this->closure->getQualifiedAncestorColumn(), '=', $id) + ->where($this->closure->getQualifiedDepthColumn(), $depthOperator, 0); } /** diff --git a/tests/migrations/2014_01_18_162506_create_entities_table.php b/tests/migrations/2014_01_18_162506_create_entities_table.php index df15437..2dce1dc 100644 --- a/tests/migrations/2014_01_18_162506_create_entities_table.php +++ b/tests/migrations/2014_01_18_162506_create_entities_table.php @@ -21,6 +21,11 @@ public function up() $table->integer('position', false, true); $table->softDeletes(); + // Ensures there are no ambiguous column names in queries involving the closure table + $table->integer('ancestor')->nullable(); + $table->integer('descendant')->nullable(); + $table->integer('depth')->nullable(); + $table->foreign('parent_id')->references('id')->on('entities')->onDelete('set null'); $table->engine = 'InnoDB';