src/Flexy/FrontBundle/Repository/ProductFrontRepository.php line 41
<?php
namespace App\Flexy\FrontBundle\Repository;
use App\Flexy\ShopBundle\Entity\Product\Product;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql as DqlFunctions;
/**
* @method Product|null find($id, $lockMode = null, $lockVersion = null)
* @method Product|null findOneBy(array $criteria, array $orderBy = null)
* @method Product[] findAll()
* @method Product[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProductFrontRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Product::class);
}
public function findNewProducts($maxResult=20)
{
return $this->createQueryBuilder('p')
->andWhere('p.isPublished = TRUE ')
->orderBy('p.id', 'DESC')
->setMaxResults($maxResult)
->getQuery()
;
}
public function findDeals($maxResult=20)
{
$now = new \DateTime();
return $this->createQueryBuilder('p')
->leftJoin("p.promotion","promotion")
->andWhere('p.isPublished = TRUE ')
->andWhere('promotion IS NOT NULL ')
->andWhere('promotion.endAt > :now ')
->setParameter("now",$now)
->orderBy('p.id', 'DESC')
->setMaxResults($maxResult)
->getQuery()
;
}
/**
* @return Product[] Returns an array of PubBanner objects
*/
public function findAll()
{
return $this->createQueryBuilder('p')
->andWhere('p.isPublished = TRUE AND user.isValid = TRUE ')
->orderBy('p.id', 'DESC')
->getQuery()
;
}
public function findByKeyWord($value,$maxResult=20)
{
return $this->createQueryBuilder('p')
->andWhere('p.isPublished = TRUE AND p.name LIKE :val')
->orWhere('p.skuCode LIKE :val')
->orWhere('p.metaTitle LIKE :val')
->orWhere('p.metaDescription LIKE :val')
->orWhere('p.description LIKE :val')
->setParameter('val', "%".$value."%")
->orderBy('p.id', 'ASC')
->setMaxResults($maxResult)
->getQuery()
;
}
public function findByKeyWordAndCategory($value,$maxResult=20)
{
return $this->createQueryBuilder('p')
->andWhere('p.isPublished = TRUE ')
->andWhere('p.name LIKE :val')
->orWhere('p.description LIKE :val')
->setParameter('val', "%".$value."%")
->orderBy('p.id', 'DESC')
->setMaxResults($maxResult)
->getQuery()
;
}
public function findByCategories($value,$maxResult=20,$isRand=true)
{
$queryBuilder = $this->createQueryBuilder('p');
$queryBuilder->leftJoin("p.parentCategory","parentCategory");
$queryBuilder->leftJoin("p.categoriesProduct","categoriesProduct");
$queryBuilder->andWhere('p.isPublished = TRUE ');
$queryBuilder->andWhere('parentCategory.name LIKE :val OR categoriesProduct.name LIKE :val OR parentCategory.slug LIKE :val OR categoriesProduct.slug LIKE :val');
$queryBuilder->setParameter('val',$value);
$queryBuilder->orderBy('p.id', 'DESC');
$queryBuilder->setMaxResults($maxResult);
return $queryBuilder->getQuery();
}
public function findBestSellers($maxResult = 20,$catogoryName=null,$createdAtLimit=null,$countryCode=null)
{
// Your logic to find the best selling products, for example:
$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction(DqlFunctions\JsonExtract::FUNCTION_NAME, DqlFunctions\JsonExtract::class);
$config->addCustomStringFunction(DqlFunctions\JsonSearch::FUNCTION_NAME, DqlFunctions\JsonSearch::class);
$params = [];
$queryBuilder = $this->createQueryBuilder('p');
$queryBuilder->leftJoin('p.orderItems', 'oi');
$queryBuilder->leftJoin('oi.parentOrder', 'o');
$queryBuilder->select('p, COUNT(oi.id) as HIDDEN soldQuantity');
if($createdAtLimit){
$queryBuilder->andWhere('o.createdAt BETWEEN :createdAtLimit AND :today' );
$params[ 'today'] = new \DateTime("today");
$params[ 'createdAtLimit'] = $createdAtLimit;
}
if($catogoryName){
$queryBuilder->leftJoin("p.parentCategory","parentCategory");
$queryBuilder->leftJoin("p.categoriesProduct","categoriesProduct");
$queryBuilder->andWhere('parentCategory.name LIKE :catogoryName OR categoriesProduct.name LIKE :catogoryName' );
$params[ 'catogoryName'] = $catogoryName;
}
$queryBuilder->groupBy('p.id');
// if($countryCode){
// $queryBuilder->addOrderBy("JSON_EXTRACT(p.metaData, :key)");
// $params[ 'key'] = "shipFromCountry";
// }
$queryBuilder->addOrderBy('soldQuantity', 'DESC');
$queryBuilder->setMaxResults($maxResult);
$queryBuilder->setParameters($params);
return $queryBuilder
->getQuery();
}
// /**
// * @return PubBanner[] Returns an array of PubBanner objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
;
}
*/
/*
public function findOneBySomeField($value): ?PubBanner
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}