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\ApgRichCategory\Entity\CategoryTrait, \Plugin\CustomerGroup\Entity\CategoryTrait, \Plugin\Schedule\Entity\CategoryTrait;
  27.         /**
  28.          * @return string
  29.          * 
  30.          */
  31.         public function __toString()
  32.         {
  33.             return (string) $this->getName();
  34.         }
  35.         /**
  36.          * @return integer
  37.          */
  38.         public function countBranches()
  39.         {
  40.             $count 1;
  41.             foreach ($this->getChildren() as $Child) {
  42.                 $count += $Child->countBranches();
  43.             }
  44.             return $count;
  45.         }
  46.         /**
  47.          * @param  \Doctrine\ORM\EntityManager $em
  48.          * @param  integer                     $sortNo
  49.          *
  50.          * @return \Eccube\Entity\Category
  51.          */
  52.         public function calcChildrenSortNo(\Doctrine\ORM\EntityManager $em$sortNo)
  53.         {
  54.             $this->setSortNo($this->getSortNo() + $sortNo);
  55.             $em->persist($this);
  56.             foreach ($this->getChildren() as $Child) {
  57.                 $Child->calcChildrenSortNo($em$sortNo);
  58.             }
  59.             return $this;
  60.         }
  61.         public function getParents()
  62.         {
  63.             $path $this->getPath();
  64.             array_pop($path);
  65.             return $path;
  66.         }
  67.         public function getPath()
  68.         {
  69.             $path = [];
  70.             $Category $this;
  71.             $max 10;
  72.             while ($max--) {
  73.                 $path[] = $Category;
  74.                 $Category $Category->getParent();
  75.                 if (!$Category || !$Category->getId()) {
  76.                     break;
  77.                 }
  78.             }
  79.             return array_reverse($path);
  80.         }
  81.         public function getNameWithLevel()
  82.         {
  83.             return str_repeat(' '$this->getHierarchy() - 1).$this->getName();
  84.         }
  85.         public function getDescendants()
  86.         {
  87.             $DescendantCategories = [];
  88.             $max 10;
  89.             $ChildCategories $this->getChildren();
  90.             foreach ($ChildCategories as $ChildCategory) {
  91.                 $DescendantCategories[$ChildCategory->getId()] = $ChildCategory;
  92.                 $DescendantCategories2 $ChildCategory->getDescendants();
  93.                 foreach ($DescendantCategories2 as $DescendantCategory) {
  94.                     $DescendantCategories[$DescendantCategory->getId()] = $DescendantCategory;
  95.                 }
  96.             }
  97.             return $DescendantCategories;
  98.         }
  99.         public function getSelfAndDescendants()
  100.         {
  101.             return array_merge([$this], $this->getDescendants());
  102.         }
  103.         /**
  104.          * カテゴリに紐づく商品があるかどうかを調べる.
  105.          *
  106.          * ProductCategoriesはExtra Lazyのため, lengthやcountで評価した際にはCOUNTのSQLが発行されるが,
  107.          * COUNT自体が重いので, LIMIT 1で取得し存在チェックを行う.
  108.          *
  109.          * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
  110.          *
  111.          * @return bool
  112.          */
  113.         public function hasProductCategories()
  114.         {
  115.             $criteria Criteria::create()
  116.             ->orderBy(['category_id' => Criteria::ASC])
  117.             ->setFirstResult(0)
  118.             ->setMaxResults(1);
  119.             return $this->ProductCategories->matching($criteria)->count() > 0;
  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.         /**
  347.          * Add child.
  348.          *
  349.          * @param \Eccube\Entity\Category $child
  350.          *
  351.          * @return Category
  352.          */
  353.         public function addChild(Category $child)
  354.         {
  355.             $this->Children[] = $child;
  356.             return $this;
  357.         }
  358.         /**
  359.          * Remove child.
  360.          *
  361.          * @param \Eccube\Entity\Category $child
  362.          *
  363.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  364.          */
  365.         public function removeChild(Category $child)
  366.         {
  367.             return $this->Children->removeElement($child);
  368.         }
  369.         /**
  370.          * Get children.
  371.          *
  372.          * @return \Doctrine\Common\Collections\Collection
  373.          */
  374.         public function getChildren()
  375.         {
  376.             return $this->Children;
  377.         }
  378.         /**
  379.          * Set parent.
  380.          *
  381.          * @param \Eccube\Entity\Category|null $parent
  382.          *
  383.          * @return Category
  384.          */
  385.         public function setParent(Category $parent null)
  386.         {
  387.             $this->Parent $parent;
  388.             return $this;
  389.         }
  390.         /**
  391.          * Get parent.
  392.          *
  393.          * @return \Eccube\Entity\Category|null
  394.          */
  395.         public function getParent()
  396.         {
  397.             return $this->Parent;
  398.         }
  399.         /**
  400.          * Set creator.
  401.          *
  402.          * @param \Eccube\Entity\Member|null $creator
  403.          *
  404.          * @return Category
  405.          */
  406.         public function setCreator(Member $creator null)
  407.         {
  408.             $this->Creator $creator;
  409.             return $this;
  410.         }
  411.         /**
  412.          * Get creator.
  413.          *
  414.          * @return \Eccube\Entity\Member|null
  415.          */
  416.         public function getCreator()
  417.         {
  418.             return $this->Creator;
  419.         }
  420.     }