Skip to content

Commit

Permalink
allow string to be passed to distinct()
Browse files Browse the repository at this point in the history
as for `select()`.
  • Loading branch information
antograssiot committed Aug 1, 2015
1 parent 5747413 commit bda04dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Database/Query.php
Expand Up @@ -299,19 +299,23 @@ public function select($fields = [], $overwrite = false)
*
* // Filters products in the same city
* $query->distinct(['city']);
* $query->distinct('city');
*
* // Filter products with the same name
* $query->distinct(['name'], true);
* $query->distinct('name', true);
* ```
*
* @param array|ExpressionInterface $on fields to be filtered on
* @param array|ExpressionInterface|string $on fields to be filtered on
* @param bool $overwrite whether to reset fields with passed list or not
* @return $this
*/
public function distinct($on = [], $overwrite = false)
{
if ($on === []) {
$on = true;
} elseif (is_string($on)) {
$on = [$on];
}

if (is_array($on)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -1543,6 +1543,21 @@ public function testSelectDistinctON()
[3, 1],
collection($results)->sortBy('author_id')->extract('author_id')->toList()
);

$query = new Query($this->connection);
$result = $query
->select(['id', 'author_id'])
->distinct('author_id')
->from(['a' => 'articles'])
->order(['author_id' => 'ASC'])
->execute();
$this->assertCount(2, $result);
$results = $result->fetchAll('assoc');
$this->assertEquals(['id', 'author_id'], array_keys($results[0]));
$this->assertEquals(
[3, 1],
collection($results)->sortBy('author_id')->extract('author_id')->toList()
);
}

/**
Expand Down

0 comments on commit bda04dd

Please sign in to comment.