From d6a96b128af30a90a473f53fda67dc2f78d60a53 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Mon, 11 Mar 2024 10:02:26 +0000 Subject: [PATCH 1/3] Fix the order relationships are evaluated in --- src/Database/Eloquent/RelationMixin.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/Database/Eloquent/RelationMixin.php b/src/Database/Eloquent/RelationMixin.php index 948a572..e2b9d2d 100644 --- a/src/Database/Eloquent/RelationMixin.php +++ b/src/Database/Eloquent/RelationMixin.php @@ -6,13 +6,12 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\HasOneOrMany; use Illuminate\Database\Eloquent\Relations\HasOneThrough; -use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphOne; -use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; use Illuminate\Database\Eloquent\Relations\MorphToMany; use LogicException; @@ -123,14 +122,14 @@ public function getRelationExistenceInQuery(): Closure }; return match (true) { - $this instanceof MorphMany => $morphOneOrMany($query, $parentQuery), - $this instanceof BelongsTo, $this instanceof MorphTo => $belongsTo($query, $parentQuery), - $this instanceof HasMany => $hasOneOrMany($query, $parentQuery), - $this instanceof HasOne => $hasOne($query, $parentQuery), $this instanceof MorphOne => $morphOne($query, $parentQuery), - $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), + $this instanceof MorphOneOrMany => $morphOneOrMany($query, $parentQuery), + $this instanceof BelongsTo => $belongsTo($query, $parentQuery), $this instanceof MorphToMany => $morphToMany($query, $parentQuery), - $this instanceof HasOneThrough, $this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery), + $this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery), + $this instanceof HasOne => $hasOne($query, $parentQuery), + $this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery), + $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) ) @@ -141,10 +140,9 @@ public function getRelationExistenceInQuery(): Closure public function getRelationWhereInKey(): Closure { return fn (): string => match (true) { - $this instanceof BelongsTo, $this instanceof MorphTo => $this->getQualifiedForeignKeyName(), - $this instanceof HasOne, $this instanceof HasMany, $this instanceof BelongsToMany, - $this instanceof MorphMany, $this instanceof MorphOne, $this instanceof MorphToMany => $this->getQualifiedParentKeyName(), - $this instanceof HasOneThrough, $this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(), + $this instanceof BelongsTo => $this->getQualifiedForeignKeyName(), + $this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(), + $this instanceof HasOneThrough => $this->getQualifiedLocalKeyName(), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) ) From 2a5c23cada50effc7a4cfcffe4c0ac5eb7dc6c37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 12 Mar 2024 12:14:19 +0800 Subject: [PATCH 2/3] fixed and reordered --- src/Database/Eloquent/RelationMixin.php | 64 +++++++++++++------------ 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Database/Eloquent/RelationMixin.php b/src/Database/Eloquent/RelationMixin.php index e2b9d2d..f8eaf7b 100644 --- a/src/Database/Eloquent/RelationMixin.php +++ b/src/Database/Eloquent/RelationMixin.php @@ -23,6 +23,7 @@ public function getRelationExistenceInQuery(): Closure $relation = function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder { return $query->select($columns); }; + // basic builder $belongsTo = function (Builder $query, Builder $parentQuery) use ($relation): Builder { $columns = $query->qualifyColumn($this->ownerKey); @@ -62,32 +63,6 @@ public function getRelationExistenceInQuery(): Closure return $relation($query, $parentQuery, $columns); }; - $hasOne = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { - if ($this->isOneOfMany()) { - $this->mergeOneOfManyJoinsTo($query); - } - - return $hasOneOrMany($query, $parentQuery); - }; - $morphOneOrMany = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { - return $hasOneOrMany($query, $parentQuery)->where( - $query->qualifyColumn($this->getMorphType()), - $this->morphClass - ); - }; - $morphOne = function (Builder $query, Builder $parentQuery) use ($morphOneOrMany): Builder { - if ($this->isOneOfMany()) { - $this->mergeOneOfManyJoinsTo($query); - } - - return $morphOneOrMany($query, $parentQuery); - }; - $morphToMany = function (Builder $query, Builder $parentQuery) use ($belongsToMany): Builder { - return $belongsToMany($query, $parentQuery)->where( - $this->qualifyPivotColumn($this->morphType), - $this->morphClass - ); - }; $hasManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder { $columns = $this->getQualifiedFirstKeyName(); if ($parentQuery->getQuery()->from === $query->getQuery()->from) { @@ -120,16 +95,45 @@ public function getRelationExistenceInQuery(): Closure return $relation($query, $parentQuery, $columns); }; + // extended builder + $hasOne = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { + if ($this->isOneOfMany()) { + $this->mergeOneOfManyJoinsTo($query); + } + + return $hasOneOrMany($query, $parentQuery); + }; + $morphOneOrMany = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { + return $hasOneOrMany($query, $parentQuery)->where( + $query->qualifyColumn($this->getMorphType()), + $this->morphClass + ); + }; + $morphOne = function (Builder $query, Builder $parentQuery) use ($morphOneOrMany): Builder { + if ($this->isOneOfMany()) { + $this->mergeOneOfManyJoinsTo($query); + } + + return $morphOneOrMany($query, $parentQuery); + }; + $morphToMany = function (Builder $query, Builder $parentQuery) use ($belongsToMany): Builder { + return $belongsToMany($query, $parentQuery)->where( + $this->qualifyPivotColumn($this->morphType), + $this->morphClass + ); + }; return match (true) { + // extended relation + $this instanceof HasOne => $hasOne($query, $parentQuery), $this instanceof MorphOne => $morphOne($query, $parentQuery), $this instanceof MorphOneOrMany => $morphOneOrMany($query, $parentQuery), - $this instanceof BelongsTo => $belongsTo($query, $parentQuery), $this instanceof MorphToMany => $morphToMany($query, $parentQuery), + // basic relation + $this instanceof BelongsTo => $belongsTo($query, $parentQuery), + $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), $this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery), - $this instanceof HasOne => $hasOne($query, $parentQuery), $this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery), - $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) ) @@ -142,7 +146,7 @@ public function getRelationWhereInKey(): Closure return fn (): string => match (true) { $this instanceof BelongsTo => $this->getQualifiedForeignKeyName(), $this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(), - $this instanceof HasOneThrough => $this->getQualifiedLocalKeyName(), + $this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) ) From cc9c1a2dd0609af0f18ebb64c05c44a2a5502063 Mon Sep 17 00:00:00 2001 From: biiiiiigmonster Date: Tue, 12 Mar 2024 04:16:08 +0000 Subject: [PATCH 3/3] Fix styling --- src/Database/Eloquent/RelationMixin.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Database/Eloquent/RelationMixin.php b/src/Database/Eloquent/RelationMixin.php index f8eaf7b..03cc03a 100644 --- a/src/Database/Eloquent/RelationMixin.php +++ b/src/Database/Eloquent/RelationMixin.php @@ -9,7 +9,6 @@ use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOneOrMany; -use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; use Illuminate\Database\Eloquent\Relations\MorphToMany;