src/Eccube/Controller/TopController.php line 34

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. use Doctrine\Common\Collections\Criteria;
  16. use Eccube\Repository\ProductRepository;
  17. use Eccube\Form\Type\AddCartType;
  18. class TopController extends AbstractController
  19. {
  20.     public function __construct(
  21.         ProductRepository $productRepository
  22.     ) {
  23.         $this->productRepository $productRepository;
  24.     }
  25.     /**
  26.      * @Route("/", name="homepage")
  27.      * @Template("index.twig")
  28.      */
  29.     public function index()
  30.     {
  31.         // 件数
  32.         $limit 4;
  33.         // 新着商品を取得
  34.         $query $this->productRepository->getQueryBuilderBySearchData([]);
  35.         $query->orderBy('p.create_date'Criteria::DESC)->setMaxResults($limit);
  36.         $products $query->getQuery()->getResult();
  37.         $ids = [];
  38.         foreach ($products as $Product) {
  39.             $ids[] = $Product->getId();
  40.         }
  41.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  42.         // addCart form
  43.         $forms = [];
  44.         foreach ($products as $Product) {
  45.             $builder $this->formFactory->createNamedBuilder(
  46.                 '',
  47.                 AddCartType::class,
  48.                 null,
  49.                 [
  50.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  51.                     'allow_extra_fields' => true,
  52.                 ]
  53.             );
  54.             $addCartForm $builder->getForm();
  55.             $forms[$Product->getId()] = $addCartForm->createView();
  56.         }
  57.         return [ 'products' => $products'forms' => $forms, ];
  58.     }
  59. }