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.0KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 findByMerchantQuery() :QueryBuilder
  23. {
  24. return $this->createQueryBuilder('e')
  25. ->where('e.merchant = :currentMerchant')
  26. ->setParameter('currentMerchant', $this->merchantUtils->getMerchantCurrent()->getId()) ;
  27. }
  28. public function findAll()
  29. {
  30. return $this->findBy(array());
  31. }
  32. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  33. {
  34. $className = $this->getClassMetadata()->getName();
  35. $entity = new $className;
  36. if($entity instanceof StatusInterface){
  37. if (!isset($criteria['status'])) $criteria['status'] = 1;
  38. if ($criteria['status'] === false) unset($criteria['status']);
  39. }
  40. if($entity instanceof FilterMerchantInterface){
  41. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  42. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  43. }
  44. return parent::findBy($criteria, $orderBy, $limit, $offset);
  45. }
  46. public function findOneBy(array $criteria, array $orderBy = null)
  47. {
  48. $className = $this->getClassMetadata()->getName();
  49. $entity = new $className;
  50. if($entity instanceof StatusInterface){
  51. if (!isset($criteria['status'])) $criteria['status'] = 1;
  52. if ($criteria['status'] === false) unset($criteria['status']);
  53. }
  54. if($entity instanceof FilterMerchantInterface){
  55. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  56. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  57. }
  58. return parent::findOneBy($criteria, $orderBy);
  59. }
  60. }