Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

143 Zeilen
4.2KB

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