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