src/WebBundle/Repository/ListCountryRepository.php line 210

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  4. use Doctrine\DBAL\DBALException;
  5. use Doctrine\ORM\NonUniqueResultException;
  6. use Doctrine\ORM\NoResultException;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Exception;
  9. use FlexApp\Constant\TimeConstant;
  10. use WebBundle\Entity\ListCountry;
  11. use WebBundle\Helper\App;
  12. use WebBundle\Helper\StrHelper;
  13. use WebBundle\Interfaces\ListEntityRepositoryInterface;
  14. use WebBundle\Traits\RepoTrait;
  15. class ListCountryRepository extends ServiceEntityRepository implements ListEntityRepositoryInterface
  16. {
  17.     use RepoTrait;
  18.     public function __construct(ManagerRegistry $registry)
  19.     {
  20.         parent::__construct($registryListCountry::class);
  21.     }
  22.     /**
  23.      * @param string $code
  24.      * @return ListCountry|null
  25.      * @throws NonUniqueResultException
  26.      */
  27.     public function getCountry(string $code): ?ListCountry
  28.     {
  29.         $q $this->createQueryBuilder('l')
  30.             ->andWhere('l.code=:code OR l.id=:code')
  31.             ->setParameter('code'$code)
  32.             ->setMaxResults(1);
  33.         $r $q->getQuery()
  34.             ->useQueryCache(true)
  35.             ->useResultCache(true3600);
  36.         try {
  37.             return $r->getSingleResult();
  38.         } catch (NoResultException $e) {
  39.             return null;
  40.         }
  41.     }
  42.     /**
  43.      * @param string $name
  44.      * @return ListCountry|null
  45.      * @throws NonUniqueResultException
  46.      */
  47.     public function getByNameEn($name)
  48.     {
  49.         $q $this->createQueryBuilder('l')
  50.             ->andWhere('l.nameEn=:nameEn')
  51.             ->setParameter('nameEn'StrHelper::toLower($name))
  52.             ->setMaxResults(1);
  53.         $r $q->getQuery()
  54.             ->useQueryCache(true)
  55.             ->useResultCache(true3600);
  56.         try {
  57.             $item $r->getSingleResult();
  58.         } catch (NoResultException $e) {
  59.             return null;
  60.         }
  61.         return $item;
  62.     }
  63.     /**
  64.      * @param bool $full
  65.      * @return array|null
  66.      */
  67.     public function getList(bool $full false): ?array
  68.     {
  69.         $q $this
  70.             ->createQueryBuilder('l');
  71.         if (!$full) {
  72.             $q->where('l.hide != 1 AND l.count > 0');
  73.         }
  74.         $r $q->orderBy('l.sort DESC, l.name')
  75.             ->getQuery()
  76.             ->useQueryCache(true)
  77.             ->useResultCache(true, (int) TimeConstant::DAY);
  78.         try {
  79.             return $r->getArrayResult();
  80.         } catch (NoResultException $e) {
  81.             return null;
  82.         }
  83.     }
  84.     /**
  85.      * @return array|null
  86.      */
  87.     public function getListForLocalize(): ?array
  88.     {
  89.         $q $this
  90.             ->createQueryBuilder('l')
  91.             ->where('l.hide != 1')
  92.             ->orderBy('l.code''ASC')
  93.             ->getQuery()
  94.             ->setResultCacheLifetime((int) TimeConstant::DAY);
  95.         try {
  96.             $items $q->getArrayResult();
  97.         } catch (NoResultException $e) {
  98.             return null;
  99.         }
  100.         return $items;
  101.     }
  102.     /**
  103.      * @return array|null
  104.      */
  105.     public function getListForByOrder()
  106.     {
  107.         $q $this
  108.             ->createQueryBuilder('lc')
  109.             ->select('lc, s')
  110.             ->leftJoin('lc.states''s')
  111.             ->orderBy('lc.sort DESC, lc.name')
  112.             ->andWhere('lc.hide != 1')
  113.             ->getQuery()
  114.             ->useQueryCache(true);
  115.         //->useResultCache(true, (3600 * 24 * 30));
  116.         try {
  117.             $items $q->getArrayResult();
  118.         } catch (NoResultException $e) {
  119.             return null;
  120.         }
  121.         return $items;
  122.     }
  123.     /**
  124.      * получить массив данных для профиля
  125.      * @return array|null
  126.      */
  127.     public function getListForProfile()
  128.     {
  129.         $q $this
  130.             ->createQueryBuilder('l');
  131.         $r $q->orderBy('l.name')
  132.             ->andWhere('l.alias is not null')
  133.             ->getQuery();
  134. //            ->useQueryCache(true)
  135. //            ->useResultCache(true, 24 * 3600);
  136.         try {
  137.             $items $r->getArrayResult();
  138.         } catch (NoResultException $e) {
  139.             return null;
  140.         }
  141.         return $items;
  142.     }
  143.     /**
  144.      * @param $sql
  145.      * @return bool
  146.      * @throws DBALException
  147.      */
  148.     public function sql($sql)
  149.     {
  150.         $conn $this->getEntityManager()->getConnection();
  151.         $conn $conn->prepare($sql);
  152.         return $conn->execute();
  153.     }
  154.     public function getListForHeader(string $locale)
  155.     {
  156.         return $this->getListForReviewsAndProfile($locale);
  157.     }
  158.     /**
  159.      * список стран для виджета отзывов
  160.      * @param string $locale
  161.      * @return array
  162.      * @throws Exception
  163.      */
  164.     public function getListForReviewsAndProfile(string $locale$withHide false)
  165.     {
  166.         $translator App::getTranslator();
  167.         $result = [];
  168.         $q $this
  169.             ->createQueryBuilder('c')
  170.             ->select('c.code, c.alias')
  171.             ->orderBy('c.sort DESC, c.name');
  172.         if (!$withHide) {
  173.             $q->andWhere('c.hide != 1');
  174.         }
  175.         $r $q->getQuery()
  176.             ->useQueryCache(true)
  177.             ->enableResultCache((int) TimeConstant::DAY);
  178.         $items $r->getArrayResult();
  179.         // Получаем переведённый список
  180.         foreach ($items as $item) {
  181.             $result[$item['code']] = $translator->trans($item['alias'], [], 'messages'$locale);
  182.         }
  183.         return $result;
  184.     }
  185.     public function getReference(int $id): ListCountry
  186.     {
  187.         return $this->getEntityManager()->getReference(ListCountry::class, $id);
  188.     }
  189.     /**
  190.      * Получает массив сущностей ListCountry по массиву кодов.
  191.      *
  192.      * @param array $codes Массив кодов стран.
  193.      * @return ListCountry[] Массив сущностей ListCountry.
  194.      */
  195.     public function getCountriesByCodes(array $codes): array
  196.     {
  197.         if (empty($codes)) {
  198.             return [];
  199.         }
  200.         $qb $this->createQueryBuilder('c')
  201.             ->where('c.code IN (:codes) OR c.id IN (:codes)')
  202.             ->setParameter('codes'$codes)
  203.             ->getQuery();
  204.         try {
  205.             return $qb->getResult();
  206.         } catch (Exception $e) {
  207.             return [];
  208.         }
  209.     }
  210. }