Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Use table aliases when calling self-referencing HasManyThrough relation #21883

Merged
merged 3 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class HasManyThrough extends Relation
*/
protected $secondLocalKey;

/**
* The count of self joins.
*
* @var int
*/
protected static $selfJoinCount = 0;

/**
* Create a new has many through relationship instance.
*
Expand Down Expand Up @@ -425,13 +432,52 @@ protected function shouldSelect(array $columns = ['*'])
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
}

$this->performJoin($query);

return $query->select($columns)->whereColumn(
$this->getExistenceCompareKey(), '=', $this->getQualifiedFirstKeyName()
);
}

/**
* Add the constraints for a relationship query on the same table.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parentQuery
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
{
$query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

$query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondLocalKey);

if ($this->throughParentSoftDeletes()) {
$query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
}

$query->getModel()->setTable($hash);

return $query->select($columns)->whereColumn(
$parentQuery->getQuery()->from.'.'.$query->getModel()->getKeyName(), '=', $this->getQualifiedFirstKeyName()
);
}

/**
* Get a relationship join table hash.
*
* @return string
*/
public function getRelationCountHash()
{
return 'laravel_reserved_'.static::$selfJoinCount++;
}

/**
* Get the key for comparing against the parent key in "has" query.
*
Expand Down
1 change: 1 addition & 0 deletions tests/Integration/Database/EloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function basic_create_and_retrieve()
$notMember = User::create(['name' => str_random()]);

$this->assertEquals([$mate1->id, $mate2->id], $user->teamMates->pluck('id')->toArray());
$this->assertEquals([$user->id], User::has('teamMates')->pluck('id')->toArray());
}
}

Expand Down