src/Eccube/Repository/CategoryRepository.php line 45

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 Eccube\Common\EccubeConfig;
  16. use Eccube\Entity\Category;
  17. use Symfony\Bridge\Doctrine\RegistryInterface;
  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.      * 引数 $Parent を指定した場合は, 指定したカテゴリの子以下を取得する.
  100.      *
  101.      * @param Category|null $Parent 指定の親カテゴリ
  102.      * @param bool $flat trueの場合, 階層化されたカテゴリを一つの配列にまとめる
  103.      *
  104.      * @return Category[] カテゴリの配列
  105.      */
  106.     public function getVisibleList(Category $Parent null$flat false$visible true)
  107.     {
  108.         $qb $this->createQueryBuilder('c1')
  109.             ->select('c1, c2, c3, c4, c5')
  110.             ->leftJoin('c1.Children''c2')
  111.             ->leftJoin('c2.Children''c3')
  112.             ->leftJoin('c3.Children''c4')
  113.             ->leftJoin('c4.Children''c5')
  114.             ->orderBy('c1.sort_no''DESC')
  115.             ->addOrderBy('c2.sort_no''DESC')
  116.             ->addOrderBy('c3.sort_no''DESC')
  117.             ->addOrderBy('c4.sort_no''DESC')
  118.             ->addOrderBy('c5.sort_no''DESC');
  119.         if ($Parent) {
  120.             $qb->where('c1.Parent = :Parent')->setParameter('Parent'$Parent);
  121.         } else {
  122.             $qb->where('c1.Parent IS NULL');
  123.         }
  124.         
  125.         $qb->andWhere('c1.visible = :visible')->setParameter('visible'$visible);
  126.         $Categories $qb->getQuery()
  127.             ->useResultCache(true$this->getCacheLifetime())
  128.             ->getResult();
  129.         if ($flat) {
  130.             $array = [];
  131.             foreach ($Categories as $Category) {
  132.                 $array array_merge($array$Category->getSelfAndDescendants());
  133.             }
  134.             $Categories $array;
  135.         }
  136.         return $Categories;
  137.     }
  138.     /**
  139.      * カテゴリを保存する.
  140.      *
  141.      * @param  Category $Category カテゴリ
  142.      */
  143.     public function save($Category)
  144.     {
  145.         if (!$Category->getId()) {
  146.             $Parent $Category->getParent();
  147.             if ($Parent) {
  148.                 $sortNo $Parent->getSortNo() - 1;
  149.             } else {
  150.                 $sortNo $this->createQueryBuilder('c')
  151.                     ->select('COALESCE(MAX(c.sort_no), 0)')
  152.                     ->getQuery()
  153.                     ->getSingleScalarResult();
  154.             }
  155.             $Category->setSortNo($sortNo 1);
  156.             $this
  157.                 ->createQueryBuilder('c')
  158.                 ->update()
  159.                 ->set('c.sort_no''c.sort_no + 1')
  160.                 ->where('c.sort_no > :sort_no')
  161.                 ->setParameter('sort_no'$sortNo)
  162.                 ->getQuery()
  163.                 ->execute();
  164.         }
  165.         $em $this->getEntityManager();
  166.         $em->persist($Category);
  167.         $em->flush($Category);
  168.     }
  169.     /**
  170.      * カテゴリを削除する.
  171.      *
  172.      * @param  Category $Category 削除対象のカテゴリ
  173.      *
  174.      * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
  175.      * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
  176.      */
  177.     public function delete($Category)
  178.     {
  179.         $this
  180.             ->createQueryBuilder('c')
  181.             ->update()
  182.             ->set('c.sort_no''c.sort_no - 1')
  183.             ->where('c.sort_no > :sort_no')
  184.             ->setParameter('sort_no'$Category->getSortNo())
  185.             ->getQuery()
  186.             ->execute();
  187.         $em $this->getEntityManager();
  188.         $em->remove($Category);
  189.         $em->flush($Category);
  190.     }
  191.     public function getListProductByCategoryId($categoryId)
  192.     {
  193.         $qb $this->createQueryBuilder('c');
  194.         $qb->addSelect(['pc''p'])
  195.             ->leftJoin('c.ProductCategories''pc')
  196.             ->leftJoin('pc.Product''p')
  197.             ->where('c.id = :categoryId')
  198.             ->setParameter('categoryId'$categoryId);
  199.         $product $qb
  200.             ->getQuery()->getResult();
  201.         return $product;
  202.     }
  203.     public function getListCategoryForSupplier($categoryParent)
  204.     {
  205.         $qb $this->createQueryBuilder('c');
  206.         $qb
  207.             ->where('c.name LIKE :categoryParent')
  208.             ->setParameter('categoryParent'$categoryParent);
  209.         $product $qb
  210.             ->getQuery()->getResult();
  211.         return $product;
  212.     }
  213. }