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.

80 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\DefaultRepositoryInterface;
  7. use Lc\ShopBundle\Context\FilterMerchantInterface;
  8. use Lc\ShopBundle\Context\GlobalParamInterface;
  9. use Lc\ShopBundle\Context\StatusInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. class BaseRepository extends EntityRepository implements ServiceEntityRepositoryInterface
  12. {
  13. public $globalParam;
  14. /**
  15. * @param string $entityClass The class name of the entity this repository manages
  16. */
  17. public function __construct(EntityManager $entityManager, GlobalParamInterface $globalParam)
  18. {
  19. $this->globalParam = $globalParam;
  20. parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
  21. }
  22. public function findByMerchantQuery($merchant)
  23. {
  24. return $this->createQueryBuilder('e')
  25. ->where('e.merchant = :currentMerchant')
  26. ->setParameter('currentMerchant', $merchant->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->globalParam->getCurrentMerchant();
  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->globalParam->getCurrentMerchant();
  56. if ($criteria['merchant'] === false) unset($criteria['merchant']);
  57. }
  58. return parent::findOneBy($criteria, $orderBy);
  59. }
  60. }