Skip to content

Commit

Permalink
Refactor withDeferredQuery interface
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Feb 21, 2024
1 parent 58fcf6b commit 2ef7f8f
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/Database/Relations/DeferOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
trait DeferOneOrMany
{
/**
* withDeferred returns the model query with deferred bindings added
* withDeferred returns a new model query with deferred bindings added, this
* will reset any constraints that come before it
* @param string|null $sessionKey
* @return \Illuminate\Database\Query\Builder
*/
Expand All @@ -24,21 +25,31 @@ public function withDeferred($sessionKey = null)
// Readd the defined constraints
$this->addDefinedConstraintsToQuery($newQuery);

return $this->withDeferredQuery($newQuery, $sessionKey);
// Apply deferred binding to the new query
$newQuery = $this->withDeferredQuery($newQuery, $sessionKey);

// Bless this query with the deferred query
$this->query->setQuery($newQuery);

// Readd the global scopes
foreach ($this->related->getGlobalScopes() as $identifier => $scope) {
$this->query->withGlobalScope($identifier, $scope);
}

return $this->query;
}

/**
* withDeferredQuery returns the model query with deferred bindings added
* @param \Illuminate\Database\Query\Builder $newQuery
* withDeferredQuery returns the supplied model query, or current model query, with
* deferred bindings added, this will preserve any constraints that came before it
* @param \Illuminate\Database\Query\Builder|null $newQuery
* @param string|null $sessionKey
* @return \Illuminate\Database\Query\Builder
*/
public function withDeferredQuery($newQuery = null, $sessionKey = null)
{
// Use case here is not wanting addDefinedConstraintsToQuery
if ($newQuery === null) {
$newQuery = $this->query->getQuery()->newQuery();
$newQuery->from($this->related->getTable());
$newQuery = $this->query->getQuery();
}

// Guess the key from the parent model
Expand All @@ -55,8 +66,10 @@ public function withDeferredQuery($newQuery = null, $sessionKey = null)
$newQuery->where(function ($query) use ($sessionKey) {
// Trick the relation to add constraints to this nested query
if ($this->parent->exists) {
$oldQuery = $this->query;
$this->query = $query;
$this->addConstraints();
$this->query = $oldQuery;
}

// Bind (Add)
Expand Down Expand Up @@ -94,15 +107,7 @@ public function withDeferredQuery($newQuery = null, $sessionKey = null)
]);
});

// Bless this query with the deferred query
$this->query->setQuery($newQuery);

// Apply global scopes
foreach ($this->related->getGlobalScopes() as $identifier => $scope) {
$this->query->withGlobalScope($identifier, $scope);
}

return $this->query;
return $newQuery;
}

/**
Expand Down

0 comments on commit 2ef7f8f

Please sign in to comment.