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