Skip to content

Commit

Permalink
Feature JoinRelationship Flexibility Initial Model #197
Browse files Browse the repository at this point in the history
  • Loading branch information
illusi03 authored Jan 27, 2025
1 parent 3c1af9b commit 62a45d3
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/Mixins/JoinRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,32 @@ public function joinRelationship(): Closure
bool $useAlias = false,
bool $disableExtraConditions = false,
?string $morphable = null,
?string $initialModel = null,
) {
$joinType = JoinsHelper::$joinMethodsMap[$joinType] ?? $joinType;
$useAlias = is_string($callback) ? false : $useAlias;
$joinHelper = JoinsHelper::make($this->getModel());
$currentModel = $initialModel ? (new $initialModel) : $this->getModel();
$joinHelper = JoinsHelper::make($currentModel);
$callback = $joinHelper->formatJoinCallback($callback);

$this->getQuery()->beforeQuery(function () use ($joinHelper) {
$joinHelper->clear();
});

if (is_null($this->getSelect())) {
$this->select(sprintf('%s.*', $this->getModel()->getTable()));
$this->select(sprintf('%s.*', $currentModel->getTable()));
}

if (Str::contains($relationName, '.')) {
$this->joinNestedRelationship($relationName, $callback, $joinType, $useAlias, $disableExtraConditions, $morphable);
$this->joinNestedRelationship(
$relationName,
$callback,
$joinType,
$useAlias,
$disableExtraConditions,
$morphable,
$initialModel
);

return $this;
}
Expand All @@ -123,7 +133,7 @@ public function joinRelationship(): Closure
$relationCallback = $callback[$relationName];
}

$relation = $this->getModel()->{$relationName}();
$relation = $currentModel->{$relationName}();
$relationQuery = $relation->getQuery();
$alias = $joinHelper->getAliasName(
$useAlias,
Expand Down Expand Up @@ -151,15 +161,15 @@ public function joinRelationship(): Closure
? "{$aliasString}.{$relationQuery->getModel()->getTable()}.{$relationName}"
: "{$relationQuery->getModel()->getTable()}.{$relationName}";

if ($joinHelper->relationshipAlreadyJoined($this->getModel(), $relationJoinCache)) {
if ($joinHelper->relationshipAlreadyJoined($currentModel, $relationJoinCache)) {
return $this;
}

if ($useAlias) {
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
}

$joinHelper->markRelationshipAsAlreadyJoined($this->getModel(), $relationJoinCache);
$joinHelper->markRelationshipAsAlreadyJoined($currentModel, $relationJoinCache);
StaticCache::clear();

$relation->performJoinForEloquentPowerJoins(
Expand Down Expand Up @@ -259,9 +269,11 @@ public function joinNestedRelationship(): Closure
bool $useAlias = false,
bool $disableExtraConditions = false,
?string $morphable = null,
?string $initialModel = null,
) {
$relations = explode('.', $relationships);
$joinHelper = JoinsHelper::make($this->getModel());
$currentModel = $initialModel ? (new $initialModel) : $this->getModel();
$joinHelper = JoinsHelper::make($currentModel);
/** @var Relation */
$latestRelation = null;

Expand All @@ -270,7 +282,7 @@ public function joinNestedRelationship(): Closure
$part[] = $relationName;
$fullRelationName = join('.', $part);

$currentModel = $latestRelation ? $latestRelation->getModel() : $this->getModel();
$currentModel = $latestRelation ? $latestRelation->getModel() : $currentModel;
$relation = $currentModel->{$relationName}();
$relationCallback = null;

Expand Down Expand Up @@ -319,7 +331,7 @@ public function joinNestedRelationship(): Closure
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
}

if ($joinHelper->relationshipAlreadyJoined($this->getModel(), $relationJoinCache)) {
if ($joinHelper->relationshipAlreadyJoined($currentModel, $relationJoinCache)) {
$latestRelation = $relation;

continue;
Expand All @@ -335,7 +347,7 @@ public function joinNestedRelationship(): Closure
);

$latestRelation = $relation;
$joinHelper->markRelationshipAsAlreadyJoined($this->getModel(), $relationJoinCache);
$joinHelper->markRelationshipAsAlreadyJoined($currentModel, $relationJoinCache);
}

StaticCache::clear();
Expand Down

0 comments on commit 62a45d3

Please sign in to comment.