Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

128 lines
3.9KB

  1. <?php
  2. namespace Lc\SovBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
  5. use Doctrine\ORM\AbstractQuery;
  6. use Doctrine\ORM\Query;
  7. use Doctrine\ORM\QueryBuilder;
  8. use Gedmo\Translatable\TranslatableListener;
  9. abstract class AbstractRepository extends ServiceEntityRepository implements ServiceEntityRepositoryInterface,
  10. AbstractRepositoryInterface
  11. {
  12. protected $defaultLocale;
  13. public function setDefaultLocale($locale)
  14. {
  15. $this->defaultLocale = $locale;
  16. }
  17. public function findOneByOldUrl($url)
  18. {
  19. $qb = $this->createQueryBuilder('entity')
  20. ->where('entity.oldUrls LIKE :oldUrl')
  21. ->andWhere('entity.status = 1')
  22. ->setParameter(':oldUrl', '%' . $url . '%');
  23. return $qb->getQuery()->getOneOrNullResult();
  24. }
  25. public function getOneOrNullResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
  26. {
  27. return $this->getTranslatedQuery($qb, $locale)->getOneOrNullResult($hydrationMode);
  28. }
  29. public function getResult(QueryBuilder $qb, $locale = null, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
  30. {
  31. return $this->getTranslatedQuery($qb, $locale)->getResult($hydrationMode);
  32. }
  33. public function getArrayResult(QueryBuilder $qb, $locale = null)
  34. {
  35. return $this->getTranslatedQuery($qb, $locale)->getArrayResult();
  36. }
  37. public function getSingleResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
  38. {
  39. return $this->getTranslatedQuery($qb, $locale)->getSingleResult($hydrationMode);
  40. }
  41. public function getScalarResult(QueryBuilder $qb, $locale = null)
  42. {
  43. return $this->getTranslatedQuery($qb, $locale)->getScalarResult();
  44. }
  45. public function getSingleScalarResult(QueryBuilder $qb, $locale = null)
  46. {
  47. return $this->getTranslatedQuery($qb, $locale)->getSingleScalarResult();
  48. }
  49. protected function getTranslatedQuery(QueryBuilder $qb, $locale = null)
  50. {
  51. $locale = null === $locale ? $this->defaultLocale : $locale;
  52. $query = $qb->getQuery();
  53. $query->setHint(
  54. Query::HINT_CUSTOM_OUTPUT_WALKER,
  55. 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
  56. );
  57. $query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);
  58. return $query;
  59. }
  60. public function findBySimilarSlug($slug)
  61. {
  62. $qb = $this->createQueryBuilder('entity')
  63. ->select('COUNT(entity.slug) as totalSimilar')
  64. ->andWhere('entity.status>=0')
  65. ->andWhere('entity.slug LIKE :slug')
  66. ->setParameter('slug', $slug)
  67. ->groupBy('entity.slug')
  68. ->having('COUNT(entity.slug) >=1');
  69. return $qb->getQuery()->getOneOrNullResult();
  70. }
  71. public function findAll()
  72. {
  73. return $this->findBy(array());
  74. }
  75. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  76. {
  77. $criteria = $this->setCriteria($criteria);
  78. return parent::findBy($criteria, $orderBy, $limit, $offset);
  79. }
  80. public function findOneBy(array $criteria, array $orderBy = null)
  81. {
  82. $criteria = $this->setCriteria($criteria);
  83. return parent::findOneBy($criteria, $orderBy);
  84. }
  85. protected function setCriteria(array $criteria): array
  86. {
  87. /*$className = $this->getClassMetadata()->getName();
  88. $entity = new $className;
  89. if ($entity instanceof StatusInterface) {
  90. if (!isset($criteria['status'])) {
  91. $criteria['status'] = 1;
  92. }
  93. if ($criteria['status'] === false) {
  94. unset($criteria['status']);
  95. }
  96. }*/
  97. return $criteria;
  98. }
  99. }