Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Admin\Agenda\Model\Repository;
- use
- Nette\Object,
- Dibi\Connection,
- App\Admin\Agenda\Model\Filters\ExampleFilter,
- App\Admin\Agenda\Model\Entity\ExampleEntity;
- use Ublaboo\DataGrid\DataSource\IDataSource;
- class ExampleRepository extends Object implements IDataSource
- {
- /**
- * @var Connection
- */
- private $connection;
- /**
- * @var ExampleFilter
- */
- private $filter;
- /**
- * @param Connection $connection
- */
- public function __construct(Connection $connection)
- {
- $this->connection = $connection;
- }
- public function getCount()
- {
- return $this->getCountByFilter($this->filter);
- }
- /**
- * @author Petr Rebicek <rebicek.petrk@o2.cz>
- * @version 1.0.0
- * @param $data
- * @return ExampleEntity
- */
- public function getRow(array $data)
- {
- $entity = new ExampleEntity();
- $entity->hydrate($data);
- return $entity;
- }
- public function getData()
- {
- return $this->getByFilter();
- }
- public function filter(array $filters)
- {
- foreach ($filters as $f) {
- $this->filter->set??($f); // Tady nastavis ExampleFilter
- }
- }
- /**
- * @author Petr Rebicek <rebicek.petrk@o2.cz>
- * @version 1.0.0
- * @param ExampleFilter $filter
- * @return ExampleEntity[]|FALSE
- */
- public function getByFilter()
- {
- $fitler = $this->filter;
- $sql = array();
- $sql[] = "
- SELECT
- id,
- user,
- a,
- b,
- c
- ";
- $sql[] = "FROM example_table";
- $sql[] = "WHERE TRUE";
- if($filter->getId())
- {
- $sql[] = "AND id IN %in";
- $sql[] = $filter->getId();
- }
- if($filter->getUser())
- {
- $sql[] = "AND user = %i";
- $sql[] = $filter->getUser();
- }
- $resource = $this->connection->query($sql);
- $resource->setRowFactory(array($this, 'getRow'));
- $data = $resource->fetchAll();
- $resource->free();
- return $data;
- }
- public function getCountByFilter(ExampleFilter $filter)
- {
- $sql = array();
- $sql[] = "
- SELECT count(id) AS count
- ";
- $sql[] = "FROM example_table";
- $sql[] = "WHERE TRUE";
- if($filter->getId())
- {
- $sql[] = "AND id IN %in";
- $sql[] = $filter->getId();
- }
- if($filter->getUser())
- {
- $sql[] = "AND user = %i";
- $sql[] = $filter->getUser();
- }
- return $this->connection->query($sql)->fetch()->count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement