src/Eccube/Repository/CategoryRepository.php line 120

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\Repository;
  13. use Doctrine\DBAL\Exception\DriverException;
  14. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  15. use Doctrine\Persistence\ManagerRegistry as RegistryInterface;
  16. use Eccube\Common\EccubeConfig;
  17. use Eccube\Entity\Category;
  18. /**
  19.  * CategoryRepository
  20.  *
  21.  * This class was generated by the Doctrine ORM. Add your own custom
  22.  * repository methods below.
  23.  */
  24. class CategoryRepository extends AbstractRepository
  25. {
  26.     /**
  27.      * @var EccubeConfig
  28.      */
  29.     protected $eccubeConfig;
  30.     /**
  31.      * CategoryRepository constructor.
  32.      *
  33.      * @param RegistryInterface $registry
  34.      * @param EccubeConfig $eccubeConfig
  35.      */
  36.     public function __construct(
  37.         RegistryInterface $registry,
  38.         EccubeConfig $eccubeConfig
  39.     ) {
  40.         parent::__construct($registryCategory::class);
  41.         $this->eccubeConfig $eccubeConfig;
  42.     }
  43.     /**
  44.      * 全カテゴリの合計を取得する.
  45.      *
  46.      * @return int 全カテゴリの合計数
  47.      */
  48.     public function getTotalCount()
  49.     {
  50.         return $this
  51.             ->createQueryBuilder('c')
  52.             ->select('COALESCE(COUNT(c.id), 0)')
  53.             ->getQuery()
  54.             ->getSingleScalarResult();
  55.     }
  56.     public function findAllWithProducts()
  57.     {
  58.         return $this->createQueryBuilder('c')
  59.             ->leftJoin('c.Children''child')
  60.             ->leftJoin('c.ProductCategories''pc')
  61.             ->leftJoin('pc.Product''p')
  62.             ->leftJoin('p.ProductClasses''pcs')
  63.             ->addSelect('child''pc''p''pcs')
  64.             ->orderBy('p.id''ASC')
  65.             ->orderBy('c.id''ASC')
  66.             ->getQuery()
  67.             ->getResult();
  68.     }
  69.     public function findSelectedProduct($id)
  70.     {
  71.         return $this->createQueryBuilder('c')
  72.             ->leftJoin('c.Children''child')
  73.             ->leftJoin('c.ProductCategories''pc')
  74.             ->leftJoin('pc.Product''p')
  75.             ->leftJoin('p.ProductClasses''pcs')
  76.             ->where('p.id = :productId')
  77.             ->setParameter('productId'$id)
  78.             ->addSelect('child''pc''p''pcs')
  79.             ->orderBy('p.id''ASC')
  80.             ->getQuery()
  81.             ->getResult();
  82.     }
  83.     /**
  84.      * カテゴリ一覧を取得する.
  85.      *
  86.      * 引数 $Parent を指定した場合は, 指定したカテゴリの子以下を取得する.
  87.      *
  88.      * @param Category|null $Parent 指定の親カテゴリ
  89.      * @param bool $flat trueの場合, 階層化されたカテゴリを一つの配列にまとめる
  90.      *
  91.      * @return Category[] カテゴリの配列
  92.      */
  93.     public function getList(Category $Parent null$flat false)
  94.     {
  95.         $qb $this->createQueryBuilder('c1')
  96.             ->select('c1, c2, c3, c4, c5')
  97.             ->leftJoin('c1.Children''c2')
  98.             ->leftJoin('c2.Children''c3')
  99.             ->leftJoin('c3.Children''c4')
  100.             ->leftJoin('c4.Children''c5')
  101.             ->orderBy('c1.sort_no''DESC')
  102.             ->addOrderBy('c2.sort_no''DESC')
  103.             ->addOrderBy('c3.sort_no''DESC')
  104.             ->addOrderBy('c4.sort_no''DESC')
  105.             ->addOrderBy('c5.sort_no''DESC');
  106.         if ($Parent) {
  107.             $qb->where('c1.Parent = :Parent')->setParameter('Parent'$Parent);
  108.         } else {
  109.             $qb->where('c1.Parent IS NULL');
  110.         }
  111.         $Categories $qb->getQuery()
  112.             ->useResultCache(true$this->getCacheLifetime())
  113.             ->getResult();
  114.         if ($flat) {
  115.             $array = [];
  116.             foreach ($Categories as $Category) {
  117.                 $array array_merge($array$Category->getSelfAndDescendants());
  118.             }
  119.             $Categories $array;
  120.         }
  121.         return $Categories;
  122.     }
  123.     /**
  124.      * カテゴリを保存する.
  125.      *
  126.      * @param  Category $Category カテゴリ
  127.      */
  128.     public function save($Category)
  129.     {
  130.         if (!$Category->getId()) {
  131.             $Parent $Category->getParent();
  132.             if ($Parent) {
  133.                 $sortNo $Parent->getSortNo() - 1;
  134.             } else {
  135.                 $sortNo $this->createQueryBuilder('c')
  136.                     ->select('COALESCE(MAX(c.sort_no), 0)')
  137.                     ->getQuery()
  138.                     ->getSingleScalarResult();
  139.             }
  140.             $Category->setSortNo($sortNo 1);
  141.             $this
  142.                 ->createQueryBuilder('c')
  143.                 ->update()
  144.                 ->set('c.sort_no''c.sort_no + 1')
  145.                 ->where('c.sort_no > :sort_no')
  146.                 ->setParameter('sort_no'$sortNo)
  147.                 ->getQuery()
  148.                 ->execute();
  149.         }
  150.         $em $this->getEntityManager();
  151.         $em->persist($Category);
  152.         $em->flush();
  153.     }
  154.     /**
  155.      * カテゴリを削除する.
  156.      *
  157.      * @param  Category $Category 削除対象のカテゴリ
  158.      *
  159.      * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
  160.      * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
  161.      */
  162.     public function delete($Category)
  163.     {
  164.         $this
  165.             ->createQueryBuilder('c')
  166.             ->update()
  167.             ->set('c.sort_no''c.sort_no - 1')
  168.             ->where('c.sort_no > :sort_no')
  169.             ->setParameter('sort_no'$Category->getSortNo())
  170.             ->getQuery()
  171.             ->execute();
  172.         $em $this->getEntityManager();
  173.         $em->remove($Category);
  174.         $em->flush();
  175.     }
  176. }