src/Repository/CategoryProductModelRepository.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Category;
  4. use Doctrine\ORM\EntityRepository;
  5. class CategoryProductModelRepository extends EntityRepository
  6. {
  7. public function searchProducts($query = '', $category, $filters=[],$sort=[],$page=1,$limit=25)
  8. {
  9. $q = $this->createQueryBuilder('cp')
  10. ->innerJoin('cp.model','p')
  11. ->where('cp.category = :category')
  12. ->setParameter('category', $category);
  13. if(!empty($query)){
  14. if(ctype_digit($query)){
  15. $q->andWhere(
  16. $q->expr()->eq('p.id',$query)
  17. );
  18. }else{
  19. $q->andWhere(
  20. 'p.model LIKE \'%'.$query.'%\''
  21. );
  22. }
  23. }
  24. if(empty($sort)){
  25. $q->orderBy('p.id', 'desc');
  26. }
  27. $q->setMaxResults($limit);
  28. $q->setFirstResult(($page-1)*$limit);
  29. $q = $q->getQuery();
  30. $products = [];
  31. $cps = $q->getResult();
  32. foreach($cps as $cp){
  33. $products[] = $cp->getProduct();
  34. }
  35. return $products;
  36. }
  37. public function searchProductsCount($query = '', $category, $filters=[])
  38. {
  39. $q = $this->createQueryBuilder('cp')
  40. ->select('count(p.id)')
  41. ->innerJoin('cp.model','p')
  42. ->where('cp.category = :category')
  43. ->setParameter('category', $category);
  44. if(!empty($query)){
  45. if(ctype_digit($query)){
  46. $q->andWhere(
  47. $q->expr()->eq('p.id',$query)
  48. );
  49. }else{
  50. $q->andWhere(
  51. 'p.model LIKE \'%'.$query.'%\''
  52. );
  53. }
  54. }
  55. $q = $q->getQuery();
  56. return $q->getSingleScalarResult();
  57. }
  58. public function getCategoriesByProduct(\App\Entity\ProductModel $product, $onlyActive=false, $limit=null, $page=1){
  59. $q = $this->createQueryBuilder('cp')
  60. ->join('cp.model', 'p')
  61. ->where('cp.model = :product')
  62. ->setParameter('product', $product);
  63. if($onlyActive){
  64. $q->andWhere('p.active = 1');
  65. }
  66. if($limit){
  67. $q->setMaxResults($limit);
  68. $q->setFirstResult(($page-1)*$limit);
  69. }
  70. $q = $q->getQuery();
  71. $results = $q->getResult();
  72. // dump($results);
  73. $output = [];
  74. foreach($results as $result){
  75. $output[] = $result->getCategory();
  76. }
  77. return $output;
  78. }
  79. public function getProductModelsByCategory(\App\Entity\Category $category, $onlyActive=false, $limit=null, $page=1, $sort = 'new', $filters = [], $excludes = []){
  80. $q = $this->createQueryBuilder('cp')
  81. ->where('cp.category = :category')
  82. ->innerJoin('cp.model','p')
  83. ->setParameter('category', $category);
  84. if($onlyActive){
  85. $q->andWhere('p.status = 1');
  86. }
  87. if($limit){
  88. $q->setMaxResults($limit);
  89. $q->setFirstResult(($page-1)*$limit);
  90. }
  91. if($excludes){
  92. $q->andWhere('cp.product not in (:excludes)');
  93. $q->setParameter('excludes', $excludes);
  94. }
  95. $q = $q->getQuery();
  96. $results = $q->getResult();
  97. // dump($results);
  98. $output = [];
  99. foreach($results as $result){
  100. $output[] = $result->getProduct();
  101. }
  102. return $output;
  103. }
  104. public function getProductModelCountByCategory(\App\Entity\Category $category, $onlyActive=false){
  105. $q = $this->createQueryBuilder('cp')
  106. ->select('count(cp.product)')
  107. ->innerJoin('cp.product','p')
  108. ->where('cp.category = :category')
  109. ->setParameter('category', $category);
  110. if($onlyActive){
  111. $q->andWhere('p.status = 1');
  112. }
  113. $q = $q->getQuery();
  114. return $q->getSingleScalarResult();
  115. }
  116. }