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

BaseRepository.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Lc\ShopBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
  4. use Doctrine\ORM\EntityManager;
  5. use Doctrine\ORM\EntityRepository;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Lc\ShopBundle\Context\FilterMerchantInterface;
  8. use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
  9. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  10. use Lc\ShopBundle\Context\StatusInterface;
  11. use Lc\ShopBundle\Services\Utils;
  12. class BaseRepository extends EntityRepository implements ServiceEntityRepositoryInterface
  13. {
  14. protected $merchantUtils;
  15. protected $utils;
  16. /**
  17. * @param string $entityClass The class name of the entity this repository manages
  18. */
  19. public function __construct(EntityManager $entityManager, MerchantUtilsInterface $merchantUtils, Utils $utils)
  20. {
  21. $this->merchantUtils = $merchantUtils;
  22. $this->utils = $utils;
  23. parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
  24. }
  25. public function findByTerm($field, $term, $limit=10){
  26. $qb = $this->findByMerchantQuery();
  27. if($this->utils->hasFilterAssociation($field, '_')){
  28. $aliasRelation = $this->utils->getFilterAssociationAlias($field, '_');
  29. $qb->innerJoin('e.'.$aliasRelation, $aliasRelation);
  30. $qb->select($this->utils->getFilterPropertyInit($field));
  31. $qb->groupBy($this->utils->getFilterPropertyInit($field));
  32. $qb->andWhere(
  33. $qb->expr()->like($this->utils->getFilterPropertyInit($field), ':term'));
  34. }else {
  35. $qb->select('e.' . $field);
  36. $qb->groupBy('e.' . $field);
  37. $qb->andWhere(
  38. $qb->expr()->like('e.' . $field, ':term'));
  39. }
  40. $qb->setParameter('term', '%'.$term.'%');
  41. $qb->setMaxResults($limit);
  42. return $qb->getQuery()->getResult();
  43. }
  44. public function findOneByOldUrl($url)
  45. {
  46. $qb = $this->createQueryBuilder('entity')
  47. ->where('entity.oldUrls LIKE :oldUrl')
  48. ->andWhere('entity.status = 1')
  49. ->setParameter(':oldUrl', '%'.$url.'%');
  50. return $qb->getQuery()->getOneOrNullResult();
  51. }
  52. public function findByMerchantQuery($merchant = null) :QueryBuilder
  53. {
  54. if($merchant === false || $merchant===null)$merchant = $this->merchantUtils->getMerchantCurrent();
  55. return $this->createQueryBuilder('e')
  56. ->where('e.merchant = :currentMerchant')
  57. ->setParameter('currentMerchant', $merchant) ;
  58. }
  59. public function findAll()
  60. {
  61. return $this->findBy(array());
  62. }
  63. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  64. {
  65. $className = $this->getClassMetadata()->getName();
  66. $entity = new $className;
  67. if($entity instanceof StatusInterface){
  68. if (!isset($criteria['status'])) $criteria['status'] = 1;
  69. if ($criteria['status'] === false) unset($criteria['status']);
  70. }
  71. if($entity instanceof FilterMerchantInterface){
  72. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  73. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  74. }
  75. return parent::findBy($criteria, $orderBy, $limit, $offset);
  76. }
  77. public function findOneBy(array $criteria, array $orderBy = null)
  78. {
  79. $className = $this->getClassMetadata()->getName();
  80. $entity = new $className;
  81. if($entity instanceof StatusInterface){
  82. if (!isset($criteria['status'])) $criteria['status'] = 1;
  83. if ($criteria['status'] === false) unset($criteria['status']);
  84. }
  85. if($entity instanceof FilterMerchantInterface){
  86. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  87. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  88. }
  89. return parent::findOneBy($criteria, $orderBy);
  90. }
  91. public function findSimilarSlug($merchant){
  92. $qb = $this->createQueryBuilder('entity')
  93. ->select('entity.id, COUNT(entity.slug) as niche')
  94. ->andWhere('entity.status>=0')
  95. ->andWhere('entity.merchant= :merchant')
  96. ->setParameter('merchant', $merchant)
  97. ->groupBy('entity.slug')
  98. ->having('COUNT(entity.slug) >1');
  99. return $qb->getQuery()->getResult();
  100. }
  101. }