src/Eccube/Controller/TopController.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class TopController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/", name="homepage")
  19.      * @Template("index.twig")
  20.      */
  21.     public function index()
  22.     {
  23.         // OPTIMIZED: Reduce products for faster loading (homepage only needs 20-30 products)
  24.         $qb $this->entityManager->getRepository('Eccube\Entity\Product')
  25.             ->createQueryBuilder('p')
  26.             ->leftJoin('Plugin\\ProductReview4\\Entity\\ProductReview''pr''WITH''p.id = pr.Product AND pr.Status = 1')
  27.             ->andWhere('p.Status = 1')
  28.             ->groupBy('p.id')
  29.             ->addSelect('COUNT(pr.id) as review_count')
  30.             ->addSelect('AVG(pr.recommend_level) as recommend_avg')
  31.             ->orderBy('COUNT(pr.id)''DESC')
  32.             ->addOrderBy('AVG(pr.recommend_level)''DESC')
  33.             ->setMaxResults(30); // Reduced from 60 to 30 for faster loading
  34.     
  35.         // OPTIMIZED: Use result cache for 5 minutes to avoid repeated queries
  36.         $results $qb->getQuery()
  37.             ->useResultCache(true300// Cache for 5 minutes
  38.             ->getResult();
  39.         
  40.         $Products = [];
  41.         $ReviewAveList = [];
  42.         $ReviewCntList = [];
  43.         
  44.         foreach ($results as $row) {
  45.             $product $row[0]; // The product entity
  46.             $Products[] = $product;
  47.             $ReviewAveList[$product->getId()] = round($row['recommend_avg'] ?? 0);
  48.             $ReviewCntList[$product->getId()] = intval($row['review_count'] ?? 0);
  49.         }
  50.     
  51.         // OPTIMIZED: Get only 3 most recent reviews (reduced query complexity)
  52.         $ProductReviews = [];
  53.         if (!empty($Products)) {
  54.             $productIds array_slice(array_map(function($product) { 
  55.                 return $product->getId(); 
  56.             }, $Products), 010); // Only check top 10 products for reviews
  57.             
  58.             $ProductReviews $this->entityManager
  59.                 ->getRepository('Plugin\ProductReview4\Entity\ProductReview')
  60.                 ->createQueryBuilder('pr')
  61.                 ->select('pr')
  62.                 ->andWhere('pr.Status = 1')
  63.                 ->andWhere('pr.Product IN (:productIds)')
  64.                 ->setParameter('productIds'$productIds)
  65.                 ->orderBy('pr.id''DESC')
  66.                 ->setMaxResults(3)
  67.                 ->getQuery()
  68.                 ->useResultCache(true300// Cache reviews for 5 minutes
  69.                 ->getResult();
  70.         }
  71.     
  72.         $Customer $this->getUser();
  73.         return [
  74.             'Products' => $Products,
  75.             'ProductReviews' => $ProductReviews,
  76.             'ReviewAveList' => $ReviewAveList,
  77.             'ReviewCntList' => $ReviewCntList,
  78.             'Customer' => $Customer,
  79.         ];
  80.     }
  81. }