Skip to content

Commit

Permalink
Query::assembleSelect(): Apply orderBy before joining the relations
Browse files Browse the repository at this point in the history
Otherwise, the query fails if the orderBy relations that are not available in `$this->with[]`.

Previously, the `$this->with` array was enhanced, but the enhanced values were not taken into account for relation Join.
  • Loading branch information
sukhwinder33445 committed Jan 22, 2025
1 parent a26d9ae commit 1fbf51e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ public function assembleSelect()
$select->where(...array_reverse($where));
}

$this->order($select);

$joinedRelations = [];
foreach ($this->getWith() + $this->getUtilize() as $path => $_) {
foreach ($resolver->resolveRelations($path) as $relationPath => $relation) {
Expand Down Expand Up @@ -548,8 +550,6 @@ public function assembleSelect()
$select->offset($this->getOffset());
}

$this->order($select);

$this->emit(static::ON_SELECT_ASSEMBLED, [$select]);

return $select;
Expand Down
57 changes: 57 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,63 @@ public function testQueryWithExpressionInOrderByThatUsesColumns()
);
}

public function testOrderByJoinRequiredRelations()
{
$query = (new Query())
->setModel(new User())
->columns(['username'])
->orderBy('user.profile.given_name', 'DESC');

$sql = <<<SQL
SELECT user.username
FROM user
INNER JOIN profile user_profile ON user_profile.user_id = user.id
ORDER BY user_profile.given_name DESC
SQL;

$this->assertSql($sql, $query->assembleSelect());
}

public function testOrderByDoNotJoinExistingRelationsAgainAndSelectAllColumnsOfWithRelation()
{
$query = (new Query())
->setModel(new User())
->with('user.profile')
->orderBy('user.profile.given_name', 'DESC');

$sql = <<<SQL
SELECT user.id,
user.username,
user.password,
user_profile.id AS user_profile_id,
user_profile.user_id AS user_profile_user_id,
user_profile.given_name AS user_profile_given_name,
user_profile.surname AS user_profile_surname
FROM user
INNER JOIN profile user_profile ON user_profile.user_id = user.id
ORDER BY user_profile.given_name DESC
SQL;

$this->assertSql($sql, $query->assembleSelect());
}

public function testQueryWithOrderByAndWithout()
{
$query = (new Query())
->setModel(new User())
->without('user.profile')
->orderBy('user.profile.given_name', 'DESC');

$sql = <<<SQL
SELECT user.id, user.username, user.password
FROM user
INNER JOIN profile user_profile ON user_profile.user_id = user.id
ORDER BY user_profile.given_name DESC
SQL;

$this->assertSql($sql, $query->assembleSelect());
}

public function testExplicitColumnsDontCauseRelationsToBeImplicitlySelected()
{
$query = (new Query())
Expand Down

0 comments on commit 1fbf51e

Please sign in to comment.