Skip to content

Commit

Permalink
Fix joinRel and wherehasIn when key is not primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
lgtaxn committed Jun 22, 2022
1 parent 3291568 commit 3a9bf71
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

3.1.1 (2022-06-22)
------------------

- Fix joinRel and wherehasIn when key is not primary key

3.1.0 (2022-02-11)
------------------

Expand Down
8 changes: 4 additions & 4 deletions src/Eloquent/JoinRelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function apply(Builder $query, $relationName, $alias = null, $callback =
$this->models[$alias] = $relation->getRelated();

$table = $relation->getRelated()->getTable().' as '.$alias;

$relation->getRelated()->setTable($alias);
$relation->getParent()->setTable($parentAlias);

Expand All @@ -85,7 +85,7 @@ public function apply(Builder $query, $relationName, $alias = null, $callback =
}

/**
* Add condition to join clause using Eloquent relationship.
* Add condition to join clause using Eloquent relationship.
*
* Supports: HasOne, HasMany, MorphOne, MorphMany, BelongsTo
*
Expand All @@ -98,11 +98,11 @@ public function apply(Builder $query, $relationName, $alias = null, $callback =
protected function addCondition(JoinClause $join, Relation $relation, $callback, $withTrashed)
{
if ($relation instanceof HasOneOrMany) {
$relationKey1 = $relation->getParent()->getQualifiedKeyName();
$relationKey1 = $relation->getParent()->getTable().'.'.$relation->getLocalKeyName();
$relationKey2 = $relation->getRelated()->getTable().'.'.$relation->getForeignKeyName();

} elseif ($relation instanceof BelongsTo) {
$relationKey1 = $relation->getRelated()->getQualifiedKeyName();
$relationKey1 = $relation->getRelated()->getTable().'.'.$relation->getOwnerKeyName();
$relationKey2 = $relation->getParent()->getTable().'.'.$relation->getForeignKeyName();

} else {
Expand Down
20 changes: 10 additions & 10 deletions src/Eloquent/Mixins/WhereHasInMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,33 @@ public function whereHasIn()
$relation = Relation::noConstraints(function () use ($relationName) {
return $this->model->{$relationName}();
});

$relationSubQuery = $relation->getQuery();

if ($callback !== null) {
$callback($relationSubQuery);
}

if ($relation instanceof HasOneOrMany) {
$relationKey1 = $relation->getParent()->getQualifiedKeyName();
$relationKey1 = $relation->getParent()->getTable().'.'.$relation->getLocalKeyName();
$relationKey2 = $relation->getRelated()->getTable().'.'.$relation->getForeignKeyName();

} elseif ($relation instanceof BelongsTo) {
$relationKey1 = $relation->getRelated()->getQualifiedKeyName();
$relationKey1 = $relation->getRelated()->getTable().'.'.$relation->getOwnerKeyName();
$relationKey2 = $relation->getParent()->getTable().'.'.$relation->getForeignKeyName();

} elseif ($relation instanceof BelongsToMany) {
$relationKey1 = $relation->getQualifiedParentKeyName();
$relationKey2 = $relation->getQualifiedForeignPivotKeyName();

} elseif ($relation instanceof HasManyThrough) {
$relationKey1 = $relation->getQualifiedLocalKeyName();
$relationKey2 = $relation->getQualifiedFirstKeyName();

} else {
throw new WhereHasInException('Relation '.get_class($relation).' not supported.');
}

return $this->whereIn(
$relationKey1,
$relationSubQuery->select($relationKey2),
Expand Down

0 comments on commit 3a9bf71

Please sign in to comment.