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