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