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.             $criteria->orderBy(['category_id' => Criteria::ASC])
  115.             ->setFirstResult(0)
  116.             ->setMaxResults(1)
  117.             ->where($criteria->expr()->eq('visible'1));
  118.             return $this->ProductCategories->matching($criteria)->count() > 0;
  119.         }
  120.         
  121.         /**
  122.          * @var int
  123.          *
  124.          * @ORM\Column(name="id", type="integer", options={"unsigned":true})
  125.          * @ORM\Id
  126.          * @ORM\GeneratedValue(strategy="IDENTITY")
  127.          */
  128.         private $id;
  129.         /**
  130.          * @var string
  131.          *
  132.          * @ORM\Column(name="category_name", type="string", length=255)
  133.          */
  134.         private $name;
  135.         /**
  136.          * @var int
  137.          *
  138.          * @ORM\Column(name="hierarchy", type="integer", options={"unsigned":true})
  139.          */
  140.         private $hierarchy;
  141.         /**
  142.          * @var int
  143.          *
  144.          * @ORM\Column(name="sort_no", type="integer")
  145.          */
  146.         private $sort_no;
  147.         /**
  148.          * @var \DateTime
  149.          *
  150.          * @ORM\Column(name="create_date", type="datetimetz")
  151.          */
  152.         private $create_date;
  153.         /**
  154.          * @var \DateTime
  155.          *
  156.          * @ORM\Column(name="update_date", type="datetimetz")
  157.          */
  158.         private $update_date;
  159.         /**
  160.          * @var \Doctrine\Common\Collections\Collection
  161.          *
  162.          * @ORM\OneToMany(targetEntity="Eccube\Entity\ProductCategory", mappedBy="Category", fetch="EXTRA_LAZY")
  163.          */
  164.         private $ProductCategories;
  165.         /**
  166.          * @var \Doctrine\Common\Collections\Collection
  167.          *
  168.          * @ORM\OneToMany(targetEntity="Eccube\Entity\Category", mappedBy="Parent")
  169.          * @ORM\OrderBy({
  170.          *     "sort_no"="DESC"
  171.          * })
  172.          */
  173.         private $Children;
  174.         /**
  175.          * @var \Eccube\Entity\Category
  176.          *
  177.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Category", inversedBy="Children")
  178.          * @ORM\JoinColumns({
  179.          *   @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
  180.          * })
  181.          */
  182.         private $Parent;
  183.         /**
  184.          * @var \Eccube\Entity\Member
  185.          *
  186.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
  187.          * @ORM\JoinColumns({
  188.          *   @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
  189.          * })
  190.          */
  191.         private $Creator;
  192.         /**
  193.          * Constructor
  194.          */
  195.         public function __construct()
  196.         {
  197.             $this->ProductCategories = new \Doctrine\Common\Collections\ArrayCollection();
  198.             $this->Children = new \Doctrine\Common\Collections\ArrayCollection();
  199.         }
  200.         /**
  201.          * Get id.
  202.          *
  203.          * @return int
  204.          */
  205.         public function getId()
  206.         {
  207.             return $this->id;
  208.         }
  209.         /**
  210.          * Set name.
  211.          *
  212.          * @param string $name
  213.          *
  214.          * @return Category
  215.          */
  216.         public function setName($name)
  217.         {
  218.             $this->name $name;
  219.             return $this;
  220.         }
  221.         /**
  222.          * Get name.
  223.          *
  224.          * @return string
  225.          */
  226.         public function getName()
  227.         {
  228.             return $this->name;
  229.         }
  230.         /**
  231.          * Set hierarchy.
  232.          *
  233.          * @param int $hierarchy
  234.          *
  235.          * @return Category
  236.          */
  237.         public function setHierarchy($hierarchy)
  238.         {
  239.             $this->hierarchy $hierarchy;
  240.             return $this;
  241.         }
  242.         /**
  243.          * Get hierarchy.
  244.          *
  245.          * @return int
  246.          */
  247.         public function getHierarchy()
  248.         {
  249.             return $this->hierarchy;
  250.         }
  251.         /**
  252.          * Set sortNo.
  253.          *
  254.          * @param int $sortNo
  255.          *
  256.          * @return Category
  257.          */
  258.         public function setSortNo($sortNo)
  259.         {
  260.             $this->sort_no $sortNo;
  261.             return $this;
  262.         }
  263.         /**
  264.          * Get sortNo.
  265.          *
  266.          * @return int
  267.          */
  268.         public function getSortNo()
  269.         {
  270.             return $this->sort_no;
  271.         }
  272.         /**
  273.          * Set createDate.
  274.          *
  275.          * @param \DateTime $createDate
  276.          *
  277.          * @return Category
  278.          */
  279.         public function setCreateDate($createDate)
  280.         {
  281.             $this->create_date $createDate;
  282.             return $this;
  283.         }
  284.         /**
  285.          * Get createDate.
  286.          *
  287.          * @return \DateTime
  288.          */
  289.         public function getCreateDate()
  290.         {
  291.             return $this->create_date;
  292.         }
  293.         /**
  294.          * Set updateDate.
  295.          *
  296.          * @param \DateTime $updateDate
  297.          *
  298.          * @return Category
  299.          */
  300.         public function setUpdateDate($updateDate)
  301.         {
  302.             $this->update_date $updateDate;
  303.             return $this;
  304.         }
  305.         /**
  306.          * Get updateDate.
  307.          *
  308.          * @return \DateTime
  309.          */
  310.         public function getUpdateDate()
  311.         {
  312.             return $this->update_date;
  313.         }
  314.         /**
  315.          * Add productCategory.
  316.          *
  317.          * @param \Eccube\Entity\ProductCategory $productCategory
  318.          *
  319.          * @return Category
  320.          */
  321.         public function addProductCategory(ProductCategory $productCategory)
  322.         {
  323.             $this->ProductCategories[] = $productCategory;
  324.             return $this;
  325.         }
  326.         /**
  327.          * Remove productCategory.
  328.          *
  329.          * @param \Eccube\Entity\ProductCategory $productCategory
  330.          *
  331.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  332.          */
  333.         public function removeProductCategory(ProductCategory $productCategory)
  334.         {
  335.             return $this->ProductCategories->removeElement($productCategory);
  336.         }
  337.         /**
  338.          * Get productCategories.
  339.          *
  340.          * @return \Doctrine\Common\Collections\Collection
  341.          */
  342.         public function getProductCategories()
  343.         {
  344.             return $this->ProductCategories;
  345.         }
  346.         public function setProductCategories($ProductCategories)
  347.         {
  348.             $this->ProductCategories $ProductCategories;
  349.         }
  350.         /**
  351.          * Add child.
  352.          *
  353.          * @param \Eccube\Entity\Category $child
  354.          *
  355.          * @return Category
  356.          */
  357.         public function addChild(Category $child)
  358.         {
  359.             $this->Children[] = $child;
  360.             return $this;
  361.         }
  362.         /**
  363.          * Remove child.
  364.          *
  365.          * @param \Eccube\Entity\Category $child
  366.          *
  367.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  368.          */
  369.         public function removeChild(Category $child)
  370.         {
  371.             return $this->Children->removeElement($child);
  372.         }
  373.         /**
  374.          * Get children.
  375.          *
  376.          * @return \Doctrine\Common\Collections\Collection
  377.          */
  378.         public function getChildren()
  379.         {
  380.             return $this->Children;
  381.         }
  382.         public function setChildren($Child)
  383.         {
  384.             $this->Children $Child;
  385.         }
  386.         /**
  387.          * Set parent.
  388.          *
  389.          * @param \Eccube\Entity\Category|null $parent
  390.          *
  391.          * @return Category
  392.          */
  393.         public function setParent(Category $parent null)
  394.         {
  395.             $this->Parent $parent;
  396.             return $this;
  397.         }
  398.         /**
  399.          * Get parent.
  400.          *
  401.          * @return \Eccube\Entity\Category|null
  402.          */
  403.         public function getParent()
  404.         {
  405.             return $this->Parent;
  406.         }
  407.         /**
  408.          * Set creator.
  409.          *
  410.          * @param \Eccube\Entity\Member|null $creator
  411.          *
  412.          * @return Category
  413.          */
  414.         public function setCreator(Member $creator null)
  415.         {
  416.             $this->Creator $creator;
  417.             return $this;
  418.         }
  419.         /**
  420.          * Get creator.
  421.          *
  422.          * @return \Eccube\Entity\Member|null
  423.          */
  424.         public function getCreator()
  425.         {
  426.             return $this->Creator;
  427.         }
  428.     }
  429. }