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.

115 lines
4.1KB

  1. <?php
  2. namespace Lc\AdminBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\ORM\EntityRepository;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Lc\AdminBundle\IModel\Cms\StatusInterface;
  8. abstract class BaseRepository extends EntityRepository implements ServiceEntityRepositoryInterface, BaseRepositoryInterface
  9. {
  10. protected $merchantUtils;
  11. protected $utils;
  12. public function __construct(EntityManagerInterface $entityManager)
  13. {
  14. parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
  15. }
  16. public function findByTerm($field, $term, $limit=10){
  17. $qb = $this->findByMerchantQuery();
  18. if($this->utils->hasFilterAssociation($field, '_')){
  19. $aliasRelation = $this->utils->getFilterAssociationAlias($field, '_');
  20. $qb->innerJoin('e.'.$aliasRelation, $aliasRelation);
  21. $qb->select($this->utils->getFilterPropertyInit($field));
  22. $qb->groupBy($this->utils->getFilterPropertyInit($field));
  23. $qb->andWhere(
  24. $qb->expr()->like($this->utils->getFilterPropertyInit($field), ':term'));
  25. }else {
  26. $qb->select('e.' . $field);
  27. $qb->groupBy('e.' . $field);
  28. $qb->andWhere(
  29. $qb->expr()->like('e.' . $field, ':term'));
  30. }
  31. $qb->setParameter('term', '%'.$term.'%');
  32. $qb->setMaxResults($limit);
  33. return $qb->getQuery()->getResult();
  34. }
  35. public function findOneByOldUrl($url)
  36. {
  37. $qb = $this->createQueryBuilder('entity')
  38. ->where('entity.oldUrls LIKE :oldUrl')
  39. ->andWhere('entity.status = 1')
  40. ->setParameter(':oldUrl', '%'.$url.'%');
  41. return $qb->getQuery()->getOneOrNullResult();
  42. }
  43. public function findByMerchantQuery() :QueryBuilder
  44. {
  45. return $this->createQueryBuilder('e')
  46. ->where('e.merchant = :currentMerchant')
  47. ->setParameter('currentMerchant', $this->merchantUtils->getMerchantCurrent()->getId()) ;
  48. }
  49. public function findAll()
  50. {
  51. return $this->findBy(array());
  52. }
  53. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  54. {
  55. $className = $this->getClassMetadata()->getName();
  56. $entity = new $className;
  57. if($entity instanceof StatusInterface){
  58. if (!isset($criteria['status'])) $criteria['status'] = 1;
  59. if ($criteria['status'] === false) unset($criteria['status']);
  60. }
  61. return parent::findBy($criteria, $orderBy, $limit, $offset);
  62. }
  63. public function findOneBy(array $criteria, array $orderBy = null)
  64. {
  65. $className = $this->getClassMetadata()->getName();
  66. $entity = new $className;
  67. if($entity instanceof StatusInterface){
  68. if (!isset($criteria['status'])) $criteria['status'] = 1;
  69. if ($criteria['status'] === false) unset($criteria['status']);
  70. }
  71. return parent::findOneBy($criteria, $orderBy);
  72. }
  73. public function findSimilarSlug($merchant){
  74. $qb = $this->createQueryBuilder('entity')
  75. ->select('entity.id, COUNT(entity.slug) as niche')
  76. ->andWhere('entity.status>=0')
  77. ->andWhere('entity.merchant= :merchant')
  78. ->setParameter('merchant', $merchant)
  79. ->groupBy('entity.slug')
  80. ->having('COUNT(entity.slug) >1');
  81. return $qb->getQuery()->getResult();
  82. }
  83. }