src/Flexy/FrontBundle/Repository/ProductFrontRepository.php line 44

  1. <?php
  2. namespace App\Flexy\FrontBundle\Repository;
  3. use App\Flexy\ShopBundle\Entity\Product\Product;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql as DqlFunctions;
  7. /**
  8.  * @method Product|null find($id, $lockMode = null, $lockVersion = null)
  9.  * @method Product|null findOneBy(array $criteria, array $orderBy = null)
  10.  * @method Product[]    findAll()
  11.  * @method Product[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  12.  */
  13. class ProductFrontRepository extends ServiceEntityRepository
  14. {
  15.     public function __construct(ManagerRegistry $registry)
  16.     {
  17.         parent::__construct($registryProduct::class);
  18.     }
  19.     public function findNewProducts($maxResult=20)
  20.     {
  21.         
  22.         return $this->createQueryBuilder('p')
  23.             ->andWhere('p.isPublished = TRUE ')
  24.             ->orderBy('p.id''DESC')
  25.             ->setMaxResults($maxResult)
  26.             ->getQuery()
  27.             
  28.         ;
  29.     }
  30.     public function findDeals($maxResult=20)
  31.     {
  32.         $now = new \DateTime();
  33.         return $this->createQueryBuilder('p')
  34.             ->leftJoin("p.promotion","promotion")
  35.             ->andWhere('p.isPublished = TRUE ')
  36.             ->andWhere('promotion IS NOT NULL ')
  37.             ->andWhere('promotion.endAt > :now ')
  38.             ->setParameter("now",$now)
  39.             ->orderBy('p.id''DESC')
  40.             ->setMaxResults($maxResult)
  41.             ->getQuery()
  42.             
  43.         ;
  44.     }
  45.     /**
  46.      * @return Product[] Returns an array of PubBanner objects
  47.     */
  48.     public function findAll()
  49.     {
  50.         return $this->createQueryBuilder('p')
  51.         
  52.         
  53.             ->andWhere('p.isPublished = TRUE AND user.isValid = TRUE ')
  54.             ->orderBy('p.id''DESC')
  55.             ->getQuery()
  56.             
  57.         ;
  58.     }
  59.     public function findByKeyWord($value,$maxResult=20)
  60.     {
  61.         return $this->createQueryBuilder('p')
  62.             ->andWhere('p.isPublished = TRUE AND p.name LIKE :val')
  63.             ->orWhere('p.skuCode LIKE :val')
  64.             ->orWhere('p.metaTitle LIKE :val')
  65.             ->orWhere('p.metaDescription LIKE :val')
  66.             ->orWhere('p.description LIKE :val')
  67.             ->setParameter('val'"%".$value."%")
  68.             ->orderBy('p.id''ASC')
  69.             ->setMaxResults($maxResult)
  70.             ->getQuery()
  71.             
  72.         ;
  73.     }
  74.     public function findByKeyWordAndCategory($value,$maxResult=20)
  75.     {
  76.         return $this->createQueryBuilder('p')
  77.             ->andWhere('p.isPublished = TRUE  ')
  78.             ->andWhere('p.name LIKE :val')
  79.             ->orWhere('p.description LIKE :val')
  80.             ->setParameter('val'"%".$value."%")
  81.             ->orderBy('p.id''DESC')
  82.             ->setMaxResults($maxResult)
  83.             ->getQuery()
  84.             
  85.         ;
  86.     }
  87.     public function findByCategories($value,$maxResult=20,$isRand=true)
  88.     {
  89.         $queryBuilder $this->createQueryBuilder('p');
  90.         $queryBuilder->leftJoin("p.parentCategory","parentCategory");
  91.         $queryBuilder->leftJoin("p.categoriesProduct","categoriesProduct");
  92.         $queryBuilder->andWhere('p.isPublished = TRUE  ');
  93.         $queryBuilder->andWhere('parentCategory.name LIKE :val OR categoriesProduct.name LIKE :val OR parentCategory.slug LIKE :val OR categoriesProduct.slug LIKE :val');
  94.         $queryBuilder->setParameter('val',$value);
  95.         $queryBuilder->orderBy('p.id''DESC');
  96.         $queryBuilder->setMaxResults($maxResult);
  97.         return $queryBuilder->getQuery();
  98.        
  99.     }
  100.     public function findBestSellers($maxResult 20,$catogoryName=null,$createdAtLimit=null,$countryCode=null)
  101.     {
  102.         // Your logic to find the best selling products, for example:
  103.             $config = new \Doctrine\ORM\Configuration();
  104.             $config->addCustomStringFunction(DqlFunctions\JsonExtract::FUNCTION_NAMEDqlFunctions\JsonExtract::class);
  105.             $config->addCustomStringFunction(DqlFunctions\JsonSearch::FUNCTION_NAMEDqlFunctions\JsonSearch::class);
  106.             $params = [];
  107.             $queryBuilder $this->createQueryBuilder('p');
  108.             
  109.             $queryBuilder->leftJoin('p.orderItems''oi');
  110.             $queryBuilder->leftJoin('oi.parentOrder''o');
  111.             $queryBuilder->select('p, COUNT(oi.id) as HIDDEN soldQuantity');
  112.             if($createdAtLimit){
  113.                 $queryBuilder->andWhere('o.createdAt BETWEEN :createdAtLimit AND :today' );
  114.                 $params'today'] = new \DateTime("today");
  115.                 $params'createdAtLimit'] = $createdAtLimit;
  116.             }
  117.             if($catogoryName){
  118.                 $queryBuilder->leftJoin("p.parentCategory","parentCategory");
  119.                 $queryBuilder->leftJoin("p.categoriesProduct","categoriesProduct");
  120.                 $queryBuilder->andWhere('parentCategory.name LIKE :catogoryName OR categoriesProduct.name LIKE :catogoryName' );
  121.                 $params'catogoryName'] = $catogoryName;
  122.             }
  123.             
  124.             
  125.             $queryBuilder->groupBy('p.id');
  126.             // if($countryCode){
  127.                
  128.             //     $queryBuilder->addOrderBy("JSON_EXTRACT(p.metaData, :key)");
  129.             //     $params[ 'key'] = "shipFromCountry";
  130.                 
  131.             // }
  132.             $queryBuilder->addOrderBy('soldQuantity''DESC');
  133.            
  134.             
  135.             $queryBuilder->setMaxResults($maxResult);
  136.             $queryBuilder->setParameters($params);
  137.             return $queryBuilder
  138.             ->getQuery();
  139.             
  140.     }
  141.     // /**
  142.     //  * @return PubBanner[] Returns an array of PubBanner objects
  143.     //  */
  144.     /*
  145.     public function findByExampleField($value)
  146.     {
  147.         return $this->createQueryBuilder('p')
  148.             ->andWhere('p.exampleField = :val')
  149.             ->setParameter('val', $value)
  150.             ->orderBy('p.id', 'ASC')
  151.             ->setMaxResults(10)
  152.             ->getQuery()
  153.             
  154.         ;
  155.     }
  156.     */
  157.     /*
  158.     public function findOneBySomeField($value): ?PubBanner
  159.     {
  160.         return $this->createQueryBuilder('p')
  161.             ->andWhere('p.exampleField = :val')
  162.             ->setParameter('val', $value)
  163.             ->getQuery()
  164.             ->getOneOrNullResult()
  165.         ;
  166.     }
  167.     */
  168. }