Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from roxblnfk/feature/order-by-ignore-dublicates
Browse files Browse the repository at this point in the history
SelectQuery: `orderBy` twice by same field
  • Loading branch information
wolfy-j authored Mar 23, 2021
2 parents 0e163b4 + bf9f357 commit 626b7fc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Query/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SelectQuery extends ActiveQuery implements
/** @var array */
protected $columns = ['*'];

/** @var array */
/** @var string[][]|FragmentInterface[][] */
protected $orderBy = [];

/** @var array */
Expand Down Expand Up @@ -165,18 +165,32 @@ public function forUpdate(): SelectQuery
public function orderBy($expression, $direction = self::SORT_ASC): SelectQuery
{
if (!is_array($expression)) {
$this->orderBy[] = [$expression, $direction];

$this->addOrder($expression, $direction);
return $this;
}

foreach ($expression as $nested => $dir) {
$this->orderBy[] = [$nested, $dir];
$this->addOrder($nested, $dir);
}

return $this;
}

/**
* @param string|FragmentInterface $field
* @param string $order Sorting direction, ASC|DESC.
* @return self|$this
*/
private function addOrder($field, string $order): SelectQuery
{
if (!is_string($field)) {
$this->orderBy[] = [$field, $order];
} elseif (!array_key_exists($field, $this->orderBy)) {
$this->orderBy[$field] = [$field, $order];
}
return $this;
}

/**
* Column or expression to group query by.
*
Expand Down Expand Up @@ -420,7 +434,7 @@ public function getTokens(): array
'where' => $this->whereTokens,
'having' => $this->havingTokens,
'groupBy' => $this->groupBy,
'orderBy' => $this->orderBy,
'orderBy' => array_values($this->orderBy),
'limit' => $this->limit,
'offset' => $this->offset,
'union' => $this->unionTokens,
Expand Down
30 changes: 30 additions & 0 deletions tests/Database/SelectQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,36 @@ public function testOrderByDesc3(): void
);
}

public function testOrderByTwiceBySameName(): void
{
$select = $this->database
->select()
->from(['users'])
->where(['name' => 'Anton'])
->orderBy('name', 'DESC')
->orderBy('name', 'ASC');

$this->assertSameQuery(
'SELECT * FROM {users} WHERE {name} = ? ORDER BY {name} DESC',
$select
);
}

public function testOrderByTwiceBySameNameArray(): void
{
$select = $this->database
->select()
->from(['users'])
->where(['name' => 'Anton'])
->orderBy('name', 'DESC')
->orderBy(['name' => 'ASC', 'foo' => 'DESC']);

$this->assertSameQuery(
'SELECT * FROM {users} WHERE {name} = ? ORDER BY {name} DESC, {foo} DESC',
$select
);
}

public function testMultipleOrderBy(): void
{
$select = $this->database
Expand Down

0 comments on commit 626b7fc

Please sign in to comment.