You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.0KB

  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 getOneOrNullResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
  18. {
  19. return $this->getTranslatedQuery($qb, $locale)->getOneOrNullResult($hydrationMode);
  20. }
  21. public function getResult(QueryBuilder $qb, $locale = null, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
  22. {
  23. return $this->getTranslatedQuery($qb, $locale)->getResult($hydrationMode);
  24. }
  25. public function getArrayResult(QueryBuilder $qb, $locale = null)
  26. {
  27. return $this->getTranslatedQuery($qb, $locale)->getArrayResult();
  28. }
  29. public function getSingleResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
  30. {
  31. return $this->getTranslatedQuery($qb, $locale)->getSingleResult($hydrationMode);
  32. }
  33. public function getScalarResult(QueryBuilder $qb, $locale = null)
  34. {
  35. return $this->getTranslatedQuery($qb, $locale)->getScalarResult();
  36. }
  37. public function getSingleScalarResult(QueryBuilder $qb, $locale = null)
  38. {
  39. return $this->getTranslatedQuery($qb, $locale)->getSingleScalarResult();
  40. }
  41. protected function getTranslatedQuery(QueryBuilder $qb, $locale = null)
  42. {
  43. $locale = null === $locale ? $this->defaultLocale : $locale;
  44. $query = $qb->getQuery();
  45. $query->setHint(
  46. Query::HINT_CUSTOM_OUTPUT_WALKER,
  47. 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
  48. );
  49. $query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);
  50. return $query;
  51. }
  52. // @TODO : à vérifier si on peut les virer
  53. /*public function findAll()
  54. {
  55. return $this->findBy(array());
  56. }
  57. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  58. {
  59. $criteria = $this->setCriteria($criteria);
  60. return parent::findBy($criteria, $orderBy, $limit, $offset);
  61. }
  62. public function findOneBy(array $criteria, array $orderBy = null)
  63. {
  64. $criteria = $this->setCriteria($criteria);
  65. return parent::findOneBy($criteria, $orderBy);
  66. }
  67. protected function setCriteria(array $criteria): array
  68. {
  69. $className = $this->getClassMetadata()->getName();
  70. $entity = new $className;
  71. if ($entity instanceof StatusInterface) {
  72. if (!isset($criteria['status'])) {
  73. $criteria['status'] = 1;
  74. }
  75. if ($criteria['status'] === false) {
  76. unset($criteria['status']);
  77. }
  78. }
  79. return $criteria;
  80. }*/
  81. // @TODO : à réécrire dans un Store
  82. /*
  83. public function findBySimilarSlug($slug)
  84. {
  85. $qb = $this->createQueryBuilder('entity')
  86. ->select('COUNT(entity.slug) as totalSimilar')
  87. ->andWhere('entity.status>=0')
  88. ->andWhere('entity.slug LIKE :slug')
  89. ->setParameter('slug', $slug)
  90. ->groupBy('entity.slug')
  91. ->having('COUNT(entity.slug) >=1');
  92. return $qb->getQuery()->getOneOrNullResult();
  93. }
  94. public function findOneByOldUrl($url)
  95. {
  96. $qb = $this->createQueryBuilder('entity')
  97. ->where('entity.oldUrls LIKE :oldUrl')
  98. ->andWhere('entity.status = 1')
  99. ->setParameter(':oldUrl', '%' . $url . '%');
  100. return $qb->getQuery()->getOneOrNullResult();
  101. }*/
  102. }