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.

138 lines
4.6KB

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