Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

139 lines
4.7KB

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