vendor/crosiersource/crosierlib-base/src/Repository/FilterRepository.php line 28

Open in your IDE?
  1. <?php
  2. namespace CrosierSource\CrosierLibBaseBundle\Repository;
  3. use CrosierSource\CrosierLibBaseBundle\Exception\ViewException;
  4. use CrosierSource\CrosierLibBaseBundle\Utils\RepositoryUtils\FilterData;
  5. use CrosierSource\CrosierLibBaseBundle\Utils\RepositoryUtils\WhereBuilder;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\EntityRepository;
  8. use Doctrine\ORM\Query\ResultSetMapping;
  9. use Doctrine\ORM\QueryBuilder;
  10. /**
  11.  * Classe base para repositórios com pesquisa pelo padrão FilterData.
  12.  *
  13.  * @author Carlos Eduardo Pauluk
  14.  */
  15. abstract class FilterRepository extends EntityRepository
  16. {
  17.     public EntityManagerInterface $doctrine;
  18.     
  19.     public function __construct(EntityManagerInterface $registry)
  20.     {
  21.         $this->doctrine $registry;
  22.         $entityClass $this::getEntityClass();
  23.         $classMetadata $registry->getClassMetadata($entityClass);
  24.         parent::__construct($registry$classMetadata);
  25.     }
  26.     /**
  27.      * @return string
  28.      */
  29.     abstract public function getEntityClass(): string;
  30.     /**
  31.      * @param null $orderBy
  32.      * @return array|mixed
  33.      * @throws ViewException
  34.      */
  35.     public function findAll($orderBy null)
  36.     {
  37.         return $this->findByFilters(null$orderBy$start 0$limit null);
  38.     }
  39.     /**
  40.      *
  41.      *
  42.      * @param $filters
  43.      * @param null $orders (no padrão do datatables.js)
  44.      * @param int $start
  45.      * @param int $limit
  46.      * @return mixed
  47.      * @throws ViewException
  48.      */
  49.     public function findByFilters($filters$orders null$start 0$limit 10)
  50.     {
  51.         $em $this->getEntityManager();
  52.         $qb $em->createQueryBuilder();
  53.         $qb->select('e');
  54.         $this->handleFrombyFilters($qb);
  55.         WhereBuilder::build($qb$filters);
  56.         if (!$orders) {
  57.             $orders $this->getDefaultOrders();
  58.         }
  59.         if (is_array($orders)) {
  60.             foreach ($orders as $col => $dir) {
  61.                 if (strpos($col'.') === FALSE) {
  62.                     $col 'e.' $col;
  63.                 }
  64.                 if (strpos($col'jsonData') !== FALSE) {
  65.                     if (strpos($col'.dt_') !== FALSE) {
  66.                         $col 'CAST(JSON_UNQUOTE(JSON_EXTRACT(e.jsonData, \'$.' substr($col11) . '\')) AS DATE)';
  67.                     } else {
  68.                         $col 'JSON_EXTRACT(e.jsonData, \'$.' substr($col11) . '\')';
  69.                     }
  70.                 }
  71.                 $qb->addOrderBy($col$dir);
  72.             }
  73.         } else if (is_string($orders)) {
  74.             $qb->addOrderBy($orders'asc');
  75.         }
  76. //        $dql = $qb->getDql();
  77. //        $sql = $qb->getQuery()->getSQL();
  78.         $query $qb->getQuery();
  79.         $query->setFirstResult($start);
  80.         if ($limit 0) {
  81.             $query->setMaxResults($limit);
  82.         }
  83.         return $query->getResult();
  84.     }
  85.     /**
  86.      * Monta o "FROM" da query.
  87.      *
  88.      * @param QueryBuilder $qb
  89.      */
  90.     public function handleFrombyFilters(QueryBuilder $qb)
  91.     {
  92.         $qb->from($this->getEntityClass(), 'e');
  93.     }
  94.     /**
  95.      * Ordens padrão do ORDER BY.
  96.      *
  97.      * @return array
  98.      */
  99.     public function getDefaultOrders()
  100.     {
  101.         return ['e.updated' => 'desc'];
  102.     }
  103.     /**
  104.      *
  105.      *
  106.      * @param array $filtersSimpl
  107.      * @return mixed
  108.      * @throws ViewException
  109.      */
  110.     public function doCountByFiltersSimpl(array $filtersSimpl)
  111.     {
  112.         $filters = [];
  113.         foreach ($filtersSimpl as $filterSimpl) {
  114.             $filter = new FilterData($filterSimpl[0], $filterSimpl[1]);
  115.             if (isset($filterSimpl[2])) {
  116.                 $filter->setVal($filterSimpl[2]);
  117.             }
  118.             $filters[] = $filter;
  119.         }
  120.         return $this->doCountByFilters($filters);
  121.     }
  122.     /**
  123.      * Contagem de registros utilizando os filtros.
  124.      *
  125.      * @param $filters
  126.      * @return mixed
  127.      * @throws ViewException
  128.      */
  129.     public function doCountByFilters(?array $filters null)
  130.     {
  131.         $em $this->getEntityManager();
  132.         $qb $em->createQueryBuilder();
  133.         $qb->select('count(e.id)');
  134.         $this->handleFrombyFilters($qb);
  135.         WhereBuilder::build($qb$filters);
  136. //        $dql = $qb->getDql();
  137. //        $sql = $qb->getQuery()->getSQL();
  138.         $count $qb->getQuery()->getScalarResult();
  139.         return $count[0][1];
  140.     }
  141.     /**
  142.      *
  143.      *
  144.      * @param array $filtersSimpl
  145.      * @param null $orders (no padrão do datatables.js)
  146.      * @param int $start
  147.      * @param int $limit
  148.      * @return mixed
  149.      * @throws ViewException
  150.      */
  151.     public function findByFiltersSimpl(array $filtersSimpl$orders null$start 0$limit 10)
  152.     {
  153.         $filters = [];
  154.         foreach ($filtersSimpl as $filterSimpl) {
  155.             if ($filterSimpl instanceof FilterData) {
  156.                 $filters[] = $filterSimpl;
  157.             } else {
  158.                 $filter = new FilterData($filterSimpl[0], $filterSimpl[1]);
  159.                 if (isset($filterSimpl[2])) {
  160.                     $filter->setVal($filterSimpl[2]);
  161.                 }
  162.                 if (isset($filterSimpl[3])) {
  163.                     $filter->setFieldType($filterSimpl[3]);
  164.                 }
  165.                 $filters[] = $filter;
  166.             }
  167.         }
  168.         return $this->findByFilters($filters$orders$start$limit);
  169.     }
  170.     /**
  171.      * @param array $filtersSimpl
  172.      * @param null $orders
  173.      * @return mixed|null
  174.      * @throws ViewException
  175.      */
  176.     public function findOneByFiltersSimpl(array $filtersSimpl$orders null)
  177.     {
  178.         $r $this->findByFiltersSimpl($filtersSimpl$orders02);
  179.         if ($r) {
  180.             if (count($r) > 1) {
  181.                 throw new ViewException('Mais de um resultado encontrado.');
  182.             }
  183.             return $r[0];
  184.         }
  185.         return null;
  186.     }
  187.     /**
  188.      * @param array $filtersSimpl
  189.      * @param null $orders
  190.      * @return mixed|null
  191.      * @throws ViewException
  192.      */
  193.     public function findAllByFiltersSimpl(array $filtersSimpl$orders null)
  194.     {
  195.         return $this->findByFiltersSimpl($filtersSimpl$orders0, -1);
  196.     }
  197.     
  198.     /**
  199.      * @param string $field
  200.      * @return mixed
  201.      * @throws ViewException
  202.      */
  203.     public function findProx($field 'id')
  204.     {
  205.         $prox null;
  206.         try {
  207.             $tableName $this->getEntityManager()->getClassMetadata($this->getEntityClass())->getTableName();
  208.             $sql 'SELECT (max(' $field ') + 1) as prox FROM ' $tableName;
  209.             $rsm = new ResultSetMapping();
  210.             $rsm->addScalarResult('prox''prox');
  211.             $query $this->getEntityManager()->createNativeQuery($sql$rsm);
  212.             $rs $query->getResult();
  213.             $prox $rs[0]['prox'];
  214.         } catch (\Exception $e) {
  215.             throw new ViewException('Erro ao buscar o próximo "' $field '" em ' $this->getEntityClass());
  216.         }
  217.         return $prox;
  218.     }
  219. }