選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbstractRepository.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 findSimilarSlug($merchant)
  61. {
  62. $qb = $this->createQueryBuilder('entity')
  63. ->select('entity.id, COUNT(entity.slug) as niche')
  64. ->andWhere('entity.status>=0')
  65. ->groupBy('entity.slug')
  66. ->having('COUNT(entity.slug) >1');
  67. return $qb->getQuery()->getResult();
  68. }
  69. public function findAll()
  70. {
  71. return $this->findBy(array());
  72. }
  73. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  74. {
  75. $criteria = $this->setCriteria($criteria);
  76. return parent::findBy($criteria, $orderBy, $limit, $offset);
  77. }
  78. public function findOneBy(array $criteria, array $orderBy = null)
  79. {
  80. $criteria = $this->setCriteria($criteria);
  81. return parent::findOneBy($criteria, $orderBy);
  82. }
  83. protected function setCriteria(array $criteria): array
  84. {
  85. /*$className = $this->getClassMetadata()->getName();
  86. $entity = new $className;
  87. if ($entity instanceof StatusInterface) {
  88. if (!isset($criteria['status'])) {
  89. $criteria['status'] = 1;
  90. }
  91. if ($criteria['status'] === false) {
  92. unset($criteria['status']);
  93. }
  94. }*/
  95. return $criteria;
  96. }
  97. }