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

116 行
4.3KB

  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. class BaseRepository extends EntityRepository implements ServiceEntityRepositoryInterface
  12. {
  13. public $merchantUtils;
  14. /**
  15. * @param string $entityClass The class name of the entity this repository manages
  16. */
  17. public function __construct(EntityManager $entityManager, MerchantUtilsInterface $merchantUtils)
  18. {
  19. $this->merchantUtils = $merchantUtils;
  20. parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
  21. }
  22. public function findByTerm($field, $term, $limit=10){
  23. $qb = $this->findByMerchantQuery();
  24. $qb->select('e.'.$field);
  25. $qb->andWhere(
  26. $qb->expr()->like('e.'.$field, ':term'));
  27. $qb->setParameter('term', '%'.$term.'%');
  28. $qb->setMaxResults($limit);
  29. return $qb->getQuery()->getResult();
  30. }
  31. public function findOneByOldUrl($url)
  32. {
  33. $qb = $this->createQueryBuilder('entity')
  34. ->where('entity.oldUrls LIKE :oldUrl')
  35. ->andWhere('entity.status = 1')
  36. ->setParameter(':oldUrl', '%'.$url.'%');
  37. return $qb->getQuery()->getOneOrNullResult();
  38. }
  39. public function findByMerchantQuery() :QueryBuilder
  40. {
  41. return $this->createQueryBuilder('e')
  42. ->where('e.merchant = :currentMerchant')
  43. ->setParameter('currentMerchant', $this->merchantUtils->getMerchantCurrent()->getId()) ;
  44. }
  45. public function findAll()
  46. {
  47. return $this->findBy(array());
  48. }
  49. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  50. {
  51. $className = $this->getClassMetadata()->getName();
  52. $entity = new $className;
  53. if($entity instanceof StatusInterface){
  54. if (!isset($criteria['status'])) $criteria['status'] = 1;
  55. if ($criteria['status'] === false) unset($criteria['status']);
  56. }
  57. if($entity instanceof FilterMerchantInterface){
  58. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  59. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  60. }
  61. return parent::findBy($criteria, $orderBy, $limit, $offset);
  62. }
  63. public function findOneBy(array $criteria, array $orderBy = 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::findOneBy($criteria, $orderBy);
  76. }
  77. public function findSimilarSlug($merchant){
  78. $qb = $this->createQueryBuilder('entity')
  79. ->select('entity.id, COUNT(entity.slug) as niche')
  80. ->andWhere('entity.status>=0')
  81. ->andWhere('entity.merchant= :merchant')
  82. ->setParameter('merchant', $merchant)
  83. ->groupBy('entity.slug')
  84. ->having('COUNT(entity.slug) >1');
  85. return $qb->getQuery()->getResult();
  86. }
  87. }