Last active
September 8, 2015 09:44
-
-
Save brabijan/a2846040cb9b646cb439 to your computer and use it in GitHub Desktop.
o5/Grido - QueryObject data source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Grido\Components\Filters\Condition; | |
use Kdyby; | |
use Kdyby\Doctrine\QueryBuilder; | |
use Kdyby\Doctrine\QueryObject; | |
use Model\Grid\FilterableQueryObject; | |
use Model\Grid\SortableQueryObject; | |
class ExampleQuery extends QueryObject implements SortableQueryObject, FilterableQueryObject | |
{ | |
public function performGridFiltering($conditions) | |
{ | |
$this->filter[] = function (QueryBuilder $qb) use ($conditions) { | |
/** @var Condition $condition */ | |
foreach ($conditions as $condition) { | |
if ($condition->column[0] === "name") { | |
$qb->andWhere("p.name LIKE :name", $condition->value[0]); | |
} | |
} | |
}; | |
return $this; | |
} | |
public function performGridSorting($sorting) | |
{ | |
$this->select[] = function (QueryBuilder $qb) use ($sorting) { | |
foreach ($sorting as $column => $direction) { | |
if ($column === "name") { | |
$qb->addOrderBy("p.name", $direction); | |
} | |
} | |
}; | |
return $this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Model\Grid; | |
interface FilterableQueryObject | |
{ | |
public function performGridFiltering($conditions); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Model\Grid; | |
use Grido\DataSources\IDataSource; | |
use Kdyby\Doctrine\EntityDao; | |
use Kdyby\Doctrine\QueryObject; | |
use Kdyby\Doctrine\ResultSet; | |
use Nette\Object; | |
class QueryObjectSource extends Object implements IDataSource | |
{ | |
/** @var EntityDao */ | |
private $repository; | |
/** @var QueryObject */ | |
private $queryObject; | |
/** @var ResultSet */ | |
private $results; | |
public function __construct(EntityDao $repository, QueryObject $queryObject) | |
{ | |
$this->repository = $repository; | |
$this->queryObject = $queryObject; | |
} | |
/** | |
* @return int | |
*/ | |
public function getCount() | |
{ | |
return $this->repository->fetch($this->queryObject)->getTotalCount(); | |
} | |
/** | |
* @return array | |
*/ | |
public function getData() | |
{ | |
return $this->getResults(); | |
} | |
/** | |
* @param array $condition | |
* @return void | |
*/ | |
public function filter(array $condition) | |
{ | |
if (empty($condition)) { | |
return; | |
} | |
if (!$this->queryObject instanceof FilterableQueryObject) { | |
throw new \Exception("This query object is not filterable"); | |
} | |
$this->queryObject->performGridFiltering($condition); | |
} | |
/** | |
* @param int $offset | |
* @param int $limit | |
* @return void | |
*/ | |
public function limit($offset, $limit) | |
{ | |
$this->getResults()->applyPaging($offset, $limit); | |
} | |
/** | |
* @param array $sorting | |
* @return void | |
*/ | |
public function sort(array $sorting) | |
{ | |
if (empty($sorting)) { | |
return; | |
} | |
if (!$this->queryObject instanceof SortableQueryObject) { | |
throw new \Exception("This query object is not sortable"); | |
} | |
$this->queryObject->performGridSorting($sorting); | |
} | |
/** | |
* @param mixed $column | |
* @param array $conditions | |
* @param int $limit | |
* @return array | |
*/ | |
public function suggest($column, array $conditions, $limit) | |
{ | |
return []; | |
} | |
private function getResults() | |
{ | |
if (!$this->results) { | |
$this->results = $this->repository->fetch($this->queryObject); | |
} | |
return $this->results; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Model\Grid; | |
interface SortableQueryObject | |
{ | |
public function performGridSorting($sorting); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment