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.

133 lines
5.1KB

  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() :QueryBuilder
  53. {
  54. return $this->createQueryBuilder('e')
  55. ->where('e.merchant = :currentMerchant')
  56. ->setParameter('currentMerchant', $this->merchantUtils->getMerchantCurrent()->getId()) ;
  57. }
  58. public function findAll()
  59. {
  60. return $this->findBy(array());
  61. }
  62. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  63. {
  64. $className = $this->getClassMetadata()->getName();
  65. $entity = new $className;
  66. if($entity instanceof StatusInterface){
  67. if (!isset($criteria['status'])) $criteria['status'] = 1;
  68. if ($criteria['status'] === false) unset($criteria['status']);
  69. }
  70. if($entity instanceof FilterMerchantInterface){
  71. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  72. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  73. }
  74. return parent::findBy($criteria, $orderBy, $limit, $offset);
  75. }
  76. public function findOneBy(array $criteria, array $orderBy = null)
  77. {
  78. $className = $this->getClassMetadata()->getName();
  79. $entity = new $className;
  80. if($entity instanceof StatusInterface){
  81. if (!isset($criteria['status'])) $criteria['status'] = 1;
  82. if ($criteria['status'] === false) unset($criteria['status']);
  83. }
  84. if($entity instanceof FilterMerchantInterface){
  85. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  86. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  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. }