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

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