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.

BaseRepository.php 3.8KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. }