Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Admin\Agenda\Model\Repository;
  4.  
  5. use
  6.     Nette\Object,
  7.     Dibi\Connection,
  8.     App\Admin\Agenda\Model\Filters\ExampleFilter,
  9.     App\Admin\Agenda\Model\Entity\ExampleEntity;
  10. use Ublaboo\DataGrid\DataSource\IDataSource;
  11.  
  12.  
  13. class ExampleRepository extends Object implements IDataSource
  14. {
  15.  
  16.     /**
  17.      * @var Connection
  18.      */
  19.     private $connection;
  20.  
  21.     /**
  22.      * @var ExampleFilter
  23.      */
  24.     private $filter;
  25.  
  26.     /**
  27.      * @param Connection $connection
  28.      */
  29.     public function __construct(Connection $connection)
  30.     {
  31.         $this->connection = $connection;
  32.     }
  33.  
  34.  
  35.     public function getCount()
  36.     {
  37.         return $this->getCountByFilter($this->filter);
  38.     }
  39.  
  40.  
  41.     /**
  42.      * @author Petr Rebicek <rebicek.petrk@o2.cz>
  43.      * @version 1.0.0
  44.      * @param $data
  45.      * @return ExampleEntity
  46.      */
  47.     public function getRow(array $data)
  48.     {
  49.         $entity = new ExampleEntity();
  50.         $entity->hydrate($data);
  51.         return $entity;
  52.     }
  53.  
  54.  
  55.     public function getData()
  56.     {
  57.         return $this->getByFilter();
  58.     }
  59.  
  60.  
  61.     public function filter(array $filters)
  62.     {
  63.         foreach ($filters as $f) {
  64.             $this->filter->set??($f); // Tady nastavis ExampleFilter
  65.         }
  66.     }
  67.  
  68.  
  69.     /**
  70.      * @author Petr Rebicek <rebicek.petrk@o2.cz>
  71.      * @version 1.0.0
  72.      * @param ExampleFilter $filter
  73.      * @return ExampleEntity[]|FALSE
  74.      */
  75.     public function getByFilter()
  76.     {
  77.         $fitler = $this->filter;
  78.  
  79.         $sql = array();
  80.         $sql[] = "
  81.             SELECT
  82.                 id,
  83.                 user,
  84.                 a,
  85.                 b,
  86.                 c
  87.         ";
  88.         $sql[] = "FROM example_table";
  89.         $sql[] = "WHERE TRUE";
  90.  
  91.  
  92.         if($filter->getId())
  93.         {
  94.             $sql[] = "AND id IN %in";
  95.             $sql[] = $filter->getId();
  96.         }
  97.         if($filter->getUser())
  98.         {
  99.             $sql[] = "AND user = %i";
  100.             $sql[] = $filter->getUser();
  101.         }
  102.  
  103.         $resource = $this->connection->query($sql);
  104.         $resource->setRowFactory(array($this, 'getRow'));
  105.         $data = $resource->fetchAll();
  106.         $resource->free();
  107.         return $data;
  108.     }
  109.  
  110.  
  111.     public function getCountByFilter(ExampleFilter $filter)
  112.     {
  113.         $sql = array();
  114.         $sql[] = "
  115.             SELECT count(id) AS count
  116.         ";
  117.         $sql[] = "FROM example_table";
  118.         $sql[] = "WHERE TRUE";
  119.  
  120.  
  121.         if($filter->getId())
  122.         {
  123.             $sql[] = "AND id IN %in";
  124.             $sql[] = $filter->getId();
  125.         }
  126.         if($filter->getUser())
  127.         {
  128.             $sql[] = "AND user = %i";
  129.             $sql[] = $filter->getUser();
  130.         }
  131.  
  132.         return $this->connection->query($sql)->fetch()->count;
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement