<?php
namespace App\Repository;
use App\Entity\Category;
use Doctrine\ORM\EntityRepository;
class CategoryProductRepository extends EntityRepository
{
public function searchProducts($query = '', $category, $filters=[],$sort=[],$page=1,$limit=25)
{
$q = $this->createQueryBuilder('cp')
->innerJoin('cp.product','p')
->where('cp.category = :category')
// ->andWhere('p.parent IS NULL')
->setParameter('category', $category);
if(!empty($query)){
if(ctype_digit($query)){
$q->andWhere(
$q->expr()->eq('p.id',$query)
);
}else{
$q->andWhere(
'p.model LIKE \'%'.$query.'%\''
);
}
}
if(empty($sort)){
$q->orderBy('p.id', 'desc');
}else{
// foreach($sort as $s){
// $q->addOrderBy('p.'.$s->property, $s->direction);
// }
}
$q->setMaxResults($limit);
$q->setFirstResult(($page-1)*$limit);
$q = $q->getQuery();
$products = [];
$cps = $q->getResult();
foreach($cps as $cp){
$products[] = $cp->getProduct();
}
return $products;
}
public function searchProductsCount($query = '', $category, $filters=[])
{
$q = $this->createQueryBuilder('cp')
->select('count(p.id)')
->innerJoin('cp.product','p')
->where('cp.category = :category')
// ->andWhere('p.parent IS NULL')
->setParameter('category', $category);
if(!empty($query)){
if(ctype_digit($query)){
$q->andWhere(
$q->expr()->eq('p.id',$query)
);
}else{
$q->andWhere(
'p.model LIKE \'%'.$query.'%\''
);
}
}
$q = $q->getQuery();
return $q->getSingleScalarResult();
}
public function getCategoriesByProduct(\App\Entity\Product $product, $onlyActive=false, $limit=null, $page=1){
$q = $this->createQueryBuilder('cp')
->join('cp.product', 'p')
->where('cp.product = :product')
->setParameter('product', $product);
if($onlyActive){
$q->andWhere('p.active = 1');
}
if($limit){
$q->setMaxResults($limit);
$q->setFirstResult(($page-1)*$limit);
}
$q = $q->getQuery();
$results = $q->getResult();
// dump($results);
$output = [];
foreach($results as $result){
$output[] = $result->getCategory();
}
return $output;
}
public function getProductsByCategory(\App\Entity\Category $category, $onlyActive=false, $limit=null, $page=1, $sort = 'new', $filters = [], $excludes = []){
$q = $this->createQueryBuilder('cp')
->where('cp.category = :category')
->innerJoin('cp.product','p')
->setParameter('category', $category);
if($onlyActive){
$q->andWhere('p.status = 1');
}
if($limit){
$q->setMaxResults($limit);
$q->setFirstResult(($page-1)*$limit);
}
if($excludes){
$q->andWhere('cp.product not in (:excludes)');
$q->setParameter('excludes', $excludes);
}
$q = $q->getQuery();
$results = $q->getResult();
// dump($results);
$output = [];
foreach($results as $result){
$output[] = $result->getProduct();
}
return $output;
}
public function getProductCountByCategory(\App\Entity\Category $category, $onlyActive=false){
$q = $this->createQueryBuilder('cp')
->select('count(cp.product)')
->innerJoin('cp.product','p')
->where('cp.category = :category')
->setParameter('category', $category);
if($onlyActive){
$q->andWhere('p.status = 1');
}
$q = $q->getQuery();
return $q->getSingleScalarResult();
}
}