Skip to content

Commit

Permalink
DataSources: Added DISTINCT to suggestion SELECT [Closed #100]
Browse files Browse the repository at this point in the history
  • Loading branch information
o5 committed Mar 25, 2014
1 parent 84b51b3 commit 41e1626
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Grido/DataSources/DibiFluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public function sort(array $sorting)
public function suggest($column, array $conditions, $limit)
{
$fluent = clone $this->fluent;
is_string($column) && $fluent->removeClause('SELECT')->select("DISTINCT $column");

foreach ($conditions as $condition) {
$this->makeWhere($condition, $fluent);
}
Expand Down
14 changes: 9 additions & 5 deletions Grido/DataSources/Doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ public function suggest($column, array $conditions, $limit)
$qb = clone $this->qb;
$qb->setMaxResults($limit);

if (is_string($column)) {
$mapping = isset($this->filterMapping[$column])
? $this->filterMapping[$column]
: $qb->getRootAlias() . '.' . $column;

$qb->select($mapping)->distinct();
}

foreach ($conditions as $condition) {
$this->makeWhere($condition, $qb);
}
Expand All @@ -245,11 +253,7 @@ public function suggest($column, array $conditions, $limit)
$data = $qb->getQuery()->getScalarResult();
foreach ($data as $row) {
if (is_string($column)) {
$mapping = isset($this->filterMapping[$column])
? str_replace('.', '_', $this->filterMapping[$column])
: $qb->getRootAlias() . '_' . $column;

$value = (string) $row[$mapping];
$value = (string) current($row);
$items[$value] = $value;
} elseif (is_callable($column)) {
$value = (string) $column($row);
Expand Down
1 change: 1 addition & 0 deletions Grido/DataSources/NetteDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function sort(array $sorting)
public function suggest($column, array $conditions, $limit)
{
$selection = clone $this->selection;
is_string($column) && $selection->select("DISTINCT $column");
$selection->limit($limit);

foreach ($conditions as $condition) {
Expand Down
3 changes: 2 additions & 1 deletion tests/Grido/DataSources/DibiFluent.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class DibiFluentTest extends DataSourceTestCase
$grid->addColumnText('country', 'Country')
->setSortable()
->setFilterText()
->setSuggestion();
->setColumn('c.title')
->setSuggestion('title');

$grid->addFilterCheck('male', 'Only male')
->setCondition(array(
Expand Down

0 comments on commit 41e1626

Please sign in to comment.