adt / query-object-data-source
Ublaboo Datagrid data source bindings for adt/base-query query objects.
Installs: 19 336
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 15
Forks: 1
Open Issues: 0
Requires
- php: >=7.4
- adt/doctrine-components: ^2.8
- nette/di: ^2.4|^3.0
- ublaboo/datagrid: ^6.0
README
Installation
via composer:
composer require adt/query-object-data-source
and in config.neon:
extensions: - ADT\QueryObjectDataSource\DI\QueryObjectDataSourceExtension
Usage
Inject or autowire QueryObjectDataSourceFactory:
/** @var \ADT\QueryObjectDataSource\IQueryObjectDataSourceFactory @autowire */ protected $queryObjectDataSourceFactory;
Create query object and wrap it as data source:
$qo = /* create query object */; $dataSource = $this->queryObjectDataSourceFactory->create($qo, "id") ->setSortCallback(function($queryObject, \Ublaboo\DataGrid\Utils\Sorting $sorting) { $sort = $sorting->getSort(); if (!empty($sort)) { foreach ($sort as $order => $by) { $queryObject->order("e.$order", $by); } } }) ->setFilterCallback(function ($queryObject, array $filter) { foreach ($filter as $field => $value) { switch ($column) { case 'dateRange': $queryObject->byDateRange(QueryObjectDataSource::parseFilterDateRange($fieldSet)); break; case 'date': $queryObject->byDate(QueryObjectDataSource::parseFilterDate($fieldSet)); break; default: $queryObject->{'by' . $field}($value); } } }) ->setLimitCallback(function ($offset, $limit, $defaultCallback) use ($query, $itemRepository) { // This callback is not necessary, but you can do your stuff with $offset and $limit here. $defaultCallback(); // Run the default action }); $grid->setDataSource($queryObjectDataSource);
You can use per column condition and sortable callbacks as well:
$datagrid->addColumnText('email', 'entity.user.email') ->setSortable() ->setSortableCallback(function (UserQueryObject $userQuery, $email) { $userQuery->orderByEmail($email); }) ->setFilterText() ->setCondition(function (UserQueryObject $userQuery, $email) { $userQuery->searchInEmail($email); });
If you implement \ADT\QueryObjectDataSource\IQueryObject on your QueryObject,
those methods will be called when there is no per column callbacks provided.
Function searchIn($column, $value)
will be called on text fields and
equalIn($column, $value)
on other column types.