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 2.9KB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Lc\ShopBundle\Context\FilterMerchantInterface;
  7. use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
  8. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  9. use Lc\ShopBundle\Context\StatusInterface;
  10. class BaseRepository extends EntityRepository implements ServiceEntityRepositoryInterface
  11. {
  12. public $merchantUtils;
  13. /**
  14. * @param string $entityClass The class name of the entity this repository manages
  15. */
  16. public function __construct(EntityManager $entityManager, MerchantUtilsInterface $merchantUtils)
  17. {
  18. $this->merchantUtils = $merchantUtils;
  19. parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
  20. }
  21. public function findByMerchantQuery()
  22. {
  23. return $this->createQueryBuilder('e')
  24. ->where('e.merchant = :currentMerchant')
  25. ->setParameter('currentMerchant', $this->merchantUtils->getMerchantCurrent()->getId()) ;
  26. }
  27. public function findAll()
  28. {
  29. return $this->findBy(array());
  30. }
  31. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  32. {
  33. $className = $this->getClassMetadata()->getName();
  34. $entity = new $className;
  35. if($entity instanceof StatusInterface){
  36. if (!isset($criteria['status'])) $criteria['status'] = 1;
  37. if ($criteria['status'] === false) unset($criteria['status']);
  38. }
  39. if($entity instanceof FilterMerchantInterface){
  40. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  41. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  42. }
  43. return parent::findBy($criteria, $orderBy, $limit, $offset);
  44. }
  45. public function findOneBy(array $criteria, array $orderBy = null)
  46. {
  47. $className = $this->getClassMetadata()->getName();
  48. $entity = new $className;
  49. if($entity instanceof StatusInterface){
  50. if (!isset($criteria['status'])) $criteria['status'] = 1;
  51. if ($criteria['status'] === false) unset($criteria['status']);
  52. }
  53. if($entity instanceof FilterMerchantInterface){
  54. if (!isset($criteria['merchant'])) $criteria['merchant'] = $this->merchantUtils->getMerchantCurrent();
  55. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  56. }
  57. return parent::findOneBy($criteria, $orderBy);
  58. }
  59. }