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.

78 lines
2.9KB

  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. }