app/proxy/entity/src/Eccube/Entity/Category.php line 33

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