Skip to content

Commit

Permalink
Column disambiguation in closure table queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Tardot committed Sep 13, 2021
1 parent 35356b4 commit eb1e284
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/migrations/2014_01_18_162506_create_entities_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit eb1e284

Please sign in to comment.