Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .docs/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ $grid->addFilterText('custom', 'Custom search:', 'name')
});
```


When using `NetteDatabaseDataSource`, the callback receives the data source instance. Use `addWhereCondition()` to append raw SQL conditions:

```php
$grid->addFilterText('custom', 'Custom search:', 'name')
->setCondition(function(NetteDatabaseDataSource $dataSource, $value) {
$dataSource->addWhereCondition('id > ?', [strlen($value)]);
});
```

### Templates:

Filters can also have their own templates:
Expand Down
6 changes: 3 additions & 3 deletions src/DataSource/NetteDatabaseDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public function sort(Sorting $sorting): IDataSource
return $this;
}

public function getDataSource(): Explorer
public function getDataSource(): NetteDatabaseDataSource
{
return $this->connection;
return $this;
}

/**
Expand All @@ -128,7 +128,7 @@ public function getQuery(): array
/**
* @param mixed[] $params
*/
protected function addWhereCondition(string $sql, array $params = []): void
public function addWhereCondition(string $sql, array $params = []): void
{
$this->whereConditions[] = [$sql, $params];
}
Expand Down
17 changes: 16 additions & 1 deletion tests/Cases/DataSources/NetteDatabaseDataSourceTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ final class NetteDatabaseDataSourceTest extends BaseDataSourceTest
public function testGetDataSource(): void
{
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM users');
Assert::same($this->db, $s->getDataSource());
Assert::same($s, $s->getDataSource());
}

public function testGetDataCached(): void
Expand Down Expand Up @@ -308,6 +308,21 @@ final class NetteDatabaseDataSourceTest extends BaseDataSourceTest
Assert::same(['%John%', '%John%'], $params);
}

public function testCustomWhereCondition(): void
{
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM users');
$filter = new FilterText($this->grid, 'a', 'b', []);
$filter->setValue('text');
$filter->setCondition(function (NetteDatabaseDataSource $dataSource, $value): void {
$dataSource->addWhereCondition('id > ?', [strlen($value)]);
});
$s->filter([$filter]);
[$sql, $params] = $s->getQuery();

Assert::same('SELECT * FROM (SELECT * FROM users) AS datagrid_base WHERE id > ?', $sql);
Assert::same([4], $params);
}

protected function setUpDatabase(): void
{
$connection = new Connection('sqlite::memory:');
Expand Down