src/Eccube/Entity/Category.php line 29

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\Entity;
  13. use Doctrine\Common\Collections\Criteria;
  14. use Doctrine\ORM\Mapping as ORM;
  15. if (!class_exists('\Eccube\Entity\Category')) {
  16.     /**
  17.      * Category
  18.      *
  19.      * @ORM\Table(name="dtb_category")
  20.      * @ORM\InheritanceType("SINGLE_TABLE")
  21.      * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
  22.      * @ORM\HasLifecycleCallbacks()
  23.      * @ORM\Entity(repositoryClass="Eccube\Repository\CategoryRepository")
  24.      */
  25.     class Category extends \Eccube\Entity\AbstractEntity
  26.     {
  27.         /**
  28.          * @return string
  29.          */
  30.         public function __toString()
  31.         {
  32.             return (string) $this->getName();
  33.         }
  34.         /**
  35.          * @return integer
  36.          */
  37.         public function countBranches()
  38.         {
  39.             $count 1;
  40.             foreach ($this->getChildren() as $Child) {
  41.                 $count += $Child->countBranches();
  42.             }
  43.             return $count;
  44.         }
  45.         /**
  46.          * @param  \Doctrine\ORM\EntityManager $em
  47.          * @param  integer                     $sortNo
  48.          *
  49.          * @return \Eccube\Entity\Category
  50.          */
  51.         public function calcChildrenSortNo(\Doctrine\ORM\EntityManager $em$sortNo)
  52.         {
  53.             $this->setSortNo($this->getSortNo() + $sortNo);
  54.             $em->persist($this);
  55.             foreach ($this->getChildren() as $Child) {
  56.                 $Child->calcChildrenSortNo($em$sortNo);
  57.             }
  58.             return $this;
  59.         }
  60.         public function getParents()
  61.         {
  62.             $path $this->getPath();
  63.             array_pop($path);
  64.             return $path;
  65.         }
  66.         public function getPath()
  67.         {
  68.             $path = [];
  69.             $Category $this;
  70.             $max 10;
  71.             while ($max--) {
  72.                 $path[] = $Category;
  73.                 $Category $Category->getParent();
  74.                 if (!$Category || !$Category->getId()) {
  75.                     break;
  76.                 }
  77.             }
  78.             return array_reverse($path);
  79.         }
  80.         public function getNameWithLevel()
  81.         {
  82.             return str_repeat(' '$this->getHierarchy() - 1).$this->getName();
  83.         }
  84.         public function getDescendants()
  85.         {
  86.             $DescendantCategories = [];
  87.             $ChildCategories $this->getChildren();
  88.             foreach ($ChildCategories as $ChildCategory) {
  89.                 $DescendantCategories[$ChildCategory->getId()] = $ChildCategory;
  90.                 $DescendantCategories2 $ChildCategory->getDescendants();
  91.                 foreach ($DescendantCategories2 as $DescendantCategory) {
  92.                     $DescendantCategories[$DescendantCategory->getId()] = $DescendantCategory;
  93.                 }
  94.             }
  95.             return $DescendantCategories;
  96.         }
  97.         public function getSelfAndDescendants()
  98.         {
  99.             return array_merge([$this], $this->getDescendants());
  100.         }
  101.         /**
  102.          * カテゴリに紐づく商品があるかどうかを調べる.
  103.          *
  104.          * ProductCategoriesはExtra Lazyのため, lengthやcountで評価した際にはCOUNTのSQLが発行されるが,
  105.          * COUNT自体が重いので, LIMIT 1で取得し存在チェックを行う.
  106.          *
  107.          * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
  108.          *
  109.          * @return bool
  110.          */
  111.         public function hasProductCategories()
  112.         {
  113.             $criteria Criteria::create()
  114.             ->orderBy(['category_id' => Criteria::ASC])
  115.             ->setFirstResult(0)
  116.             ->setMaxResults(1);
  117.             return $this->ProductCategories->matching($criteria)->count() > 0;
  118.         }
  119.         /**
  120.          * @var int
  121.          *
  122.          * @ORM\Column(name="id", type="integer", options={"unsigned":true})
  123.          * @ORM\Id
  124.          * @ORM\GeneratedValue(strategy="IDENTITY")
  125.          */
  126.         private $id;
  127.         /**
  128.          * @var string
  129.          *
  130.          * @ORM\Column(name="category_name", type="string", length=255)
  131.          */
  132.         private $name;
  133.         /**
  134.          * @var int
  135.          *
  136.          * @ORM\Column(name="hierarchy", type="integer", options={"unsigned":true})
  137.          */
  138.         private $hierarchy;
  139.         /**
  140.          * @var int
  141.          *
  142.          * @ORM\Column(name="sort_no", type="integer")
  143.          */
  144.         private $sort_no;
  145.         /**
  146.          * @var \DateTime
  147.          *
  148.          * @ORM\Column(name="create_date", type="datetimetz")
  149.          */
  150.         private $create_date;
  151.         /**
  152.          * @var \DateTime
  153.          *
  154.          * @ORM\Column(name="update_date", type="datetimetz")
  155.          */
  156.         private $update_date;
  157.         /**
  158.          * @var \Doctrine\Common\Collections\Collection
  159.          *
  160.          * @ORM\OneToMany(targetEntity="Eccube\Entity\ProductCategory", mappedBy="Category", fetch="EXTRA_LAZY")
  161.          */
  162.         private $ProductCategories;
  163.         /**
  164.          * @var \Doctrine\Common\Collections\Collection
  165.          *
  166.          * @ORM\OneToMany(targetEntity="Eccube\Entity\Category", mappedBy="Parent")
  167.          * @ORM\OrderBy({
  168.          *     "sort_no"="DESC"
  169.          * })
  170.          */
  171.         private $Children;
  172.         /**
  173.          * @var \Eccube\Entity\Category
  174.          *
  175.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Category", inversedBy="Children")
  176.          * @ORM\JoinColumns({
  177.          *   @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
  178.          * })
  179.          */
  180.         private $Parent;
  181.         /**
  182.          * @var \Eccube\Entity\Member
  183.          *
  184.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
  185.          * @ORM\JoinColumns({
  186.          *   @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
  187.          * })
  188.          */
  189.         private $Creator;
  190.         /**
  191.          * Constructor
  192.          */
  193.         public function __construct()
  194.         {
  195.             $this->ProductCategories = new \Doctrine\Common\Collections\ArrayCollection();
  196.             $this->Children = new \Doctrine\Common\Collections\ArrayCollection();
  197.         }
  198.         /**
  199.          * Get id.
  200.          *
  201.          * @return int
  202.          */
  203.         public function getId()
  204.         {
  205.             return $this->id;
  206.         }
  207.         /**
  208.          * Set name.
  209.          *
  210.          * @param string $name
  211.          *
  212.          * @return Category
  213.          */
  214.         public function setName($name)
  215.         {
  216.             $this->name $name;
  217.             return $this;
  218.         }
  219.         /**
  220.          * Get name.
  221.          *
  222.          * @return string
  223.          */
  224.         public function getName()
  225.         {
  226.             return $this->name;
  227.         }
  228.         /**
  229.          * Set hierarchy.
  230.          *
  231.          * @param int $hierarchy
  232.          *
  233.          * @return Category
  234.          */
  235.         public function setHierarchy($hierarchy)
  236.         {
  237.             $this->hierarchy $hierarchy;
  238.             return $this;
  239.         }
  240.         /**
  241.          * Get hierarchy.
  242.          *
  243.          * @return int
  244.          */
  245.         public function getHierarchy()
  246.         {
  247.             return $this->hierarchy;
  248.         }
  249.         /**
  250.          * Set sortNo.
  251.          *
  252.          * @param int $sortNo
  253.          *
  254.          * @return Category
  255.          */
  256.         public function setSortNo($sortNo)
  257.         {
  258.             $this->sort_no $sortNo;
  259.             return $this;
  260.         }
  261.         /**
  262.          * Get sortNo.
  263.          *
  264.          * @return int
  265.          */
  266.         public function getSortNo()
  267.         {
  268.             return $this->sort_no;
  269.         }
  270.         /**
  271.          * Set createDate.
  272.          *
  273.          * @param \DateTime $createDate
  274.          *
  275.          * @return Category
  276.          */
  277.         public function setCreateDate($createDate)
  278.         {
  279.             $this->create_date $createDate;
  280.             return $this;
  281.         }
  282.         /**
  283.          * Get createDate.
  284.          *
  285.          * @return \DateTime
  286.          */
  287.         public function getCreateDate()
  288.         {
  289.             return $this->create_date;
  290.         }
  291.         /**
  292.          * Set updateDate.
  293.          *
  294.          * @param \DateTime $updateDate
  295.          *
  296.          * @return Category
  297.          */
  298.         public function setUpdateDate($updateDate)
  299.         {
  300.             $this->update_date $updateDate;
  301.             return $this;
  302.         }
  303.         /**
  304.          * Get updateDate.
  305.          *
  306.          * @return \DateTime
  307.          */
  308.         public function getUpdateDate()
  309.         {
  310.             return $this->update_date;
  311.         }
  312.         /**
  313.          * Add productCategory.
  314.          *
  315.          * @param \Eccube\Entity\ProductCategory $productCategory
  316.          *
  317.          * @return Category
  318.          */
  319.         public function addProductCategory(ProductCategory $productCategory)
  320.         {
  321.             $this->ProductCategories[] = $productCategory;
  322.             return $this;
  323.         }
  324.         /**
  325.          * Remove productCategory.
  326.          *
  327.          * @param \Eccube\Entity\ProductCategory $productCategory
  328.          *
  329.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  330.          */
  331.         public function removeProductCategory(ProductCategory $productCategory)
  332.         {
  333.             return $this->ProductCategories->removeElement($productCategory);
  334.         }
  335.         /**
  336.          * Get productCategories.
  337.          *
  338.          * @return \Doctrine\Common\Collections\Collection
  339.          */
  340.         public function getProductCategories()
  341.         {
  342.             return $this->ProductCategories;
  343.         }
  344.         /**
  345.          * Add child.
  346.          *
  347.          * @param \Eccube\Entity\Category $child
  348.          *
  349.          * @return Category
  350.          */
  351.         public function addChild(Category $child)
  352.         {
  353.             $this->Children[] = $child;
  354.             return $this;
  355.         }
  356.         /**
  357.          * Remove child.
  358.          *
  359.          * @param \Eccube\Entity\Category $child
  360.          *
  361.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  362.          */
  363.         public function removeChild(Category $child)
  364.         {
  365.             return $this->Children->removeElement($child);
  366.         }
  367.         /**
  368.          * Get children.
  369.          *
  370.          * @return \Doctrine\Common\Collections\Collection
  371.          */
  372.         public function getChildren()
  373.         {
  374.             return $this->Children;
  375.         }
  376.         /**
  377.          * Set parent.
  378.          *
  379.          * @param \Eccube\Entity\Category|null $parent
  380.          *
  381.          * @return Category
  382.          */
  383.         public function setParent(Category $parent null)
  384.         {
  385.             $this->Parent $parent;
  386.             return $this;
  387.         }
  388.         /**
  389.          * Get parent.
  390.          *
  391.          * @return \Eccube\Entity\Category|null
  392.          */
  393.         public function getParent()
  394.         {
  395.             return $this->Parent;
  396.         }
  397.         /**
  398.          * Set creator.
  399.          *
  400.          * @param \Eccube\Entity\Member|null $creator
  401.          *
  402.          * @return Category
  403.          */
  404.         public function setCreator(Member $creator null)
  405.         {
  406.             $this->Creator $creator;
  407.             return $this;
  408.         }
  409.         /**
  410.          * Get creator.
  411.          *
  412.          * @return \Eccube\Entity\Member|null
  413.          */
  414.         public function getCreator()
  415.         {
  416.             return $this->Creator;
  417.         }
  418.         /* 以下追記箇所*/
  419.         /**
  420.          * @var string|null
  421.          *
  422.          * @ORM\Column(name="free_area", type="text", nullable=true)
  423.          */
  424.         private $free_area;
  425.         /**
  426.          * @var string
  427.          *
  428.          * @ORM\Column(name="category_image", type="string", length=255, nullable=true)
  429.          */
  430.         private $category_image;
  431.         /**
  432.          * Set freeArea.
  433.          *
  434.          * @param string|null $freeArea
  435.          *
  436.          * @return Category
  437.          */
  438.         public function setFreeArea($freeArea null)
  439.         {
  440.             $this->free_area $freeArea;
  441.             return $this;
  442.         }
  443.         /**
  444.          * Get freeArea.
  445.          *
  446.          * @return string|null
  447.          */
  448.         public function getFreeArea()
  449.         {
  450.             return $this->free_area;
  451.         }
  452.         /**
  453.          * Set categoryImage.
  454.          *
  455.          * @param string|null $categoryImage
  456.          *
  457.          * @return Category
  458.          */
  459.         public function setCategoryImage($categoryImage null)
  460.         {
  461.             $this->category_image $categoryImage;
  462.             return $this;
  463.         }
  464.         /**
  465.          * Get categoryImage.
  466.          *
  467.          * @return string|null
  468.          */
  469.         public function getCategoryImage()
  470.         {
  471.             return $this->category_image;
  472.         }
  473.         /**
  474.          * あるカテゴリーの一番上位のカテゴリーを取得するメソッド
  475.          * @see https://umebius.com/eccube/get_root_category_from_one_category/
  476.          * @return Category|null
  477.          */
  478.         public function getRootCategory()
  479.         {
  480.             $Category $this;
  481.             $max 10;
  482.             while ($max--) {
  483.                 if (!$Category->getParent()) {
  484.                     return $Category;
  485.                 }
  486.                 $Category $Category->getParent();
  487.             }
  488.             return null;
  489.         }
  490.     }
  491. }