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