src/Eccube/Repository/CategoryRepository.php line 89

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.    * @var EccubeConfig
  27.    */
  28.   protected $eccubeConfig;
  29.   /**
  30.    * CategoryRepository constructor.
  31.    *
  32.    * @param RegistryInterface $registry
  33.    * @param EccubeConfig $eccubeConfig
  34.    */
  35.   public function __construct(
  36.   RegistryInterface $registryEccubeConfig $eccubeConfig
  37.   ) {
  38.     parent::__construct($registryCategory::class);
  39.     $this->eccubeConfig $eccubeConfig;
  40.   }
  41.   /**
  42.    * 子カテゴリー一覧をハッシュで取得
  43.    * @param integer $p_id
  44.    * @return hash
  45.    */
  46.   public function getHash($p_id) {
  47.     $conn $this->getEntityManager()->getConnection();
  48.     $sql "SELECT id,category_name FROM dtb_category WHERE parent_category_id='{$p_id}' ORDER BY sort_no DESC;";
  49.     $stmt $conn->prepare($sql);
  50.     $resultSet $stmt->executeQuery();
  51.     $rows $resultSet->fetchAllAssociative();
  52.     $hash = array();
  53.     foreach ($rows as $row) {
  54.       $hash[$row["id"]] = $row["category_name"];
  55.     }
  56.     return $hash;
  57.   }
  58.   /**
  59.    * 全カテゴリの合計を取得する.
  60.    *
  61.    * @return int 全カテゴリの合計数
  62.    */
  63.   public function getTotalCount() {
  64.     return $this
  65.         ->createQueryBuilder('c')
  66.         ->select('COALESCE(COUNT(c.id), 0)')
  67.         ->getQuery()
  68.         ->getSingleScalarResult();
  69.   }
  70.   /**
  71.    * カテゴリ一覧を取得する.
  72.    *
  73.    * 引数 $Parent を指定した場合は, 指定したカテゴリの子以下を取得する.
  74.    *
  75.    * @param Category|null $Parent 指定の親カテゴリ
  76.    * @param bool $flat trueの場合, 階層化されたカテゴリを一つの配列にまとめる
  77.    *
  78.    * @return Category[] カテゴリの配列
  79.    */
  80.   public function getList(Category $Parent null$flat false) {
  81.     $qb $this->createQueryBuilder('c1')
  82.       ->select('c1, c2, c3, c4, c5')
  83.       ->leftJoin('c1.Children''c2')
  84.       ->leftJoin('c2.Children''c3')
  85.       ->leftJoin('c3.Children''c4')
  86.       ->leftJoin('c4.Children''c5')
  87.       ->orderBy('c1.sort_no''DESC')
  88.       ->addOrderBy('c2.sort_no''DESC')
  89.       ->addOrderBy('c3.sort_no''DESC')
  90.       ->addOrderBy('c4.sort_no''DESC')
  91.       ->addOrderBy('c5.sort_no''DESC');
  92.     if ($Parent) {
  93.       $qb->where('c1.Parent = :Parent')->setParameter('Parent'$Parent);
  94.     } else {
  95.       $qb->where('c1.Parent IS NULL');
  96.     }
  97.     $Categories $qb->getQuery()
  98.       ->useResultCache(true$this->getCacheLifetime())
  99.       ->getResult();
  100.     if ($flat) {
  101.       $array = [];
  102.       foreach ($Categories as $Category) {
  103.         $array array_merge($array$Category->getSelfAndDescendants());
  104.       }
  105.       $Categories $array;
  106.     }
  107.     return $Categories;
  108.   }
  109.   /**
  110.    * カテゴリを保存する.
  111.    *
  112.    * @param  Category $Category カテゴリ
  113.    */
  114.   public function save($Category) {
  115.     if (!$Category->getId()) {
  116.       $Parent $Category->getParent();
  117.       if ($Parent) {
  118.         $sortNo $Parent->getSortNo() - 1;
  119.       } else {
  120.         $sortNo $this->createQueryBuilder('c')
  121.           ->select('COALESCE(MAX(c.sort_no), 0)')
  122.           ->getQuery()
  123.           ->getSingleScalarResult();
  124.       }
  125.       $Category->setSortNo($sortNo 1);
  126.       $this
  127.         ->createQueryBuilder('c')
  128.         ->update()
  129.         ->set('c.sort_no''c.sort_no + 1')
  130.         ->where('c.sort_no > :sort_no')
  131.         ->setParameter('sort_no'$sortNo)
  132.         ->getQuery()
  133.         ->execute();
  134.     }
  135.     $em $this->getEntityManager();
  136.     $em->persist($Category);
  137.     $em->flush();
  138.   }
  139.   /**
  140.    * カテゴリを削除する.
  141.    *
  142.    * @param  Category $Category 削除対象のカテゴリ
  143.    *
  144.    * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
  145.    * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
  146.    */
  147.   public function delete($Category) {
  148.     $this
  149.       ->createQueryBuilder('c')
  150.       ->update()
  151.       ->set('c.sort_no''c.sort_no - 1')
  152.       ->where('c.sort_no > :sort_no')
  153.       ->setParameter('sort_no'$Category->getSortNo())
  154.       ->getQuery()
  155.       ->execute();
  156.     $em $this->getEntityManager();
  157.     $em->remove($Category);
  158.     $em->flush();
  159.   }
  160.   /**
  161.    * 商品のカテゴリー存在チェック
  162.    * @param integer $product_id
  163.    * @param integer $category_id
  164.    * @return bool
  165.    */
  166.   public function hasCategory($product_id,$category_id) {
  167.     $conn $this->getEntityManager()->getConnection();
  168.     $sql "SELECT product_id FROM dtb_product_category WHERE product_id='{$product_id}' AND category_id='{$category_id}'";
  169.     $stmt $conn->prepare($sql);
  170.     $resultSet $stmt->executeQuery();
  171.     $row $resultSet->fetchAllAssociative();
  172.     return empty($row[0]["product_id"]) ? false true;
  173.   }
  174.   /**
  175.    * カテゴリーIDから商品一覧を取得
  176.    * @param $category_id
  177.    * @return array
  178.    * @throws \Doctrine\DBAL\Exception
  179.    */
  180.   public function getHashCategory($category_id) {
  181.     $conn $this->getEntityManager()->getConnection();
  182.     $sql "SELECT product_id FROM dtb_product_category WHERE category_id='{$category_id}'";
  183.     $stmt $conn->prepare($sql);
  184.     $resultSet $stmt->executeQuery();
  185.     $rows $resultSet->fetchAllAssociative();
  186.     $hash = array();
  187.     foreach ($rows as $row) {
  188.       $hash[$row["product_id"]] = true;
  189.     }
  190.     return $hash;
  191.   }
  192. }