src/Eccube/Repository/CategoryRepository.php line 73

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.     /**
  57.      * カテゴリ一覧を取得する.
  58.      *
  59.      * 引数 $Parent を指定した場合は, 指定したカテゴリの子以下を取得する.
  60.      *
  61.      * @param Category|null $Parent 指定の親カテゴリ
  62.      * @param bool $flat trueの場合, 階層化されたカテゴリを一つの配列にまとめる
  63.      *
  64.      * @return Category[] カテゴリの配列
  65.      */
  66.     public function getList(Category $Parent null$flat false)
  67.     {
  68.         $qb $this->createQueryBuilder('c1')
  69.             ->select('c1, c2, c3, c4, c5')
  70.             ->leftJoin('c1.Children''c2')
  71.             ->leftJoin('c2.Children''c3')
  72.             ->leftJoin('c3.Children''c4')
  73.             ->leftJoin('c4.Children''c5')
  74.             ->orderBy('c1.sort_no''DESC')
  75.             ->addOrderBy('c2.sort_no''DESC')
  76.             ->addOrderBy('c3.sort_no''DESC')
  77.             ->addOrderBy('c4.sort_no''DESC')
  78.             ->addOrderBy('c5.sort_no''DESC');
  79.         if ($Parent) {
  80.             $qb->where('c1.Parent = :Parent')->setParameter('Parent'$Parent);
  81.         } else {
  82.             $qb->where('c1.Parent IS NULL');
  83.         }
  84.         $Categories $qb->getQuery()
  85.             ->useResultCache(true$this->getCacheLifetime())
  86.             ->getResult();
  87.         if ($flat) {
  88.             $array = [];
  89.             foreach ($Categories as $Category) {
  90.                 $array array_merge($array$Category->getSelfAndDescendants());
  91.             }
  92.             $Categories $array;
  93.         }
  94.         return $Categories;
  95.     }
  96.     /**
  97.      * カテゴリを保存する.
  98.      *
  99.      * @param  Category $Category カテゴリ
  100.      */
  101.     public function save($Category)
  102.     {
  103.         if (!$Category->getId()) {
  104.             $Parent $Category->getParent();
  105.             if ($Parent) {
  106.                 $sortNo $Parent->getSortNo() - 1;
  107.             } else {
  108.                 $sortNo $this->createQueryBuilder('c')
  109.                     ->select('COALESCE(MAX(c.sort_no), 0)')
  110.                     ->getQuery()
  111.                     ->getSingleScalarResult();
  112.             }
  113.             $Category->setSortNo($sortNo 1);
  114.             $this
  115.                 ->createQueryBuilder('c')
  116.                 ->update()
  117.                 ->set('c.sort_no''c.sort_no + 1')
  118.                 ->where('c.sort_no > :sort_no')
  119.                 ->setParameter('sort_no'$sortNo)
  120.                 ->getQuery()
  121.                 ->execute();
  122.         }
  123.         $em $this->getEntityManager();
  124.         $em->persist($Category);
  125.         $em->flush();
  126.     }
  127.     /**
  128.      * カテゴリを削除する.
  129.      *
  130.      * @param  Category $Category 削除対象のカテゴリ
  131.      *
  132.      * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
  133.      * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
  134.      */
  135.     public function delete($Category)
  136.     {
  137.         $this
  138.             ->createQueryBuilder('c')
  139.             ->update()
  140.             ->set('c.sort_no''c.sort_no - 1')
  141.             ->where('c.sort_no > :sort_no')
  142.             ->setParameter('sort_no'$Category->getSortNo())
  143.             ->getQuery()
  144.             ->execute();
  145.         $em $this->getEntityManager();
  146.         $em->remove($Category);
  147.         $em->flush();
  148.     }
  149. }