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