app/Customize/Entity/CategoryTv.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 Customize\Entity;
  13. use Doctrine\Common\Collections\Criteria;
  14. use Doctrine\ORM\Mapping as ORM;
  15. if (!class_exists('\Customize\Entity\CategoryTv')) {
  16.     /**
  17.      * CategoryTv
  18.      *
  19.      * @ORM\Table(name="dtb_category_tv")
  20.      * @ORM\InheritanceType("SINGLE_TABLE")
  21.      * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
  22.      * @ORM\HasLifecycleCallbacks()
  23.      * @ORM\Entity(repositoryClass="Customize\Repository\CategoryTvRepository")
  24.      */
  25.     class CategoryTv extends \Eccube\Entity\AbstractEntity
  26.     {
  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 \Customize\Entity\CategoryTv
  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.             $CategoryTv $this;
  70.             $max 10;
  71.             while ($max--) {
  72.                 $path[] = $CategoryTv;
  73.                 $CategoryTv $CategoryTv->getParent();
  74.                 if (!$CategoryTv || !$CategoryTv->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 $ChildCategoryTv) {
  89.                 $DescendantCategories[$ChildCategoryTv->getId()] = $ChildCategoryTv;
  90.                 $DescendantCategories2 $ChildCategoryTv->getDescendants();
  91.                 foreach ($DescendantCategories2 as $DescendantCategoryTv) {
  92.                     $DescendantCategories[$DescendantCategoryTv->getId()] = $DescendantCategoryTv;
  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->ProductCategoryTvs->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 int
  135.          *
  136.          * @ORM\Column(name="hierarchy", type="integer", options={"unsigned":true})
  137.          */
  138.         private $hierarchy;
  139.         /**
  140.          * @var int
  141.          *
  142.          * @ORM\Column(name="sort_no", type="integer")
  143.          */
  144.         private $sort_no;
  145.         /**
  146.          * @var \DateTime
  147.          *
  148.          * @ORM\Column(name="create_date", type="datetimetz")
  149.          */
  150.         private $create_date;
  151.         /**
  152.          * @var \DateTime
  153.          *
  154.          * @ORM\Column(name="update_date", type="datetimetz")
  155.          */
  156.         private $update_date;
  157.         /**
  158.          * @var \Doctrine\Common\Collections\Collection
  159.          *
  160.          * @ORM\OneToMany(targetEntity="Customize\Entity\ProductCategoryTv", mappedBy="CategoryTv", fetch="EXTRA_LAZY")
  161.          */
  162.         private $ProductCategoryTvs;
  163.         /**
  164.          * @var \Doctrine\Common\Collections\Collection
  165.          *
  166.          * @ORM\OneToMany(targetEntity="Customize\Entity\CategoryTv", mappedBy="Parent")
  167.          * @ORM\OrderBy({
  168.          *     "sort_no"="DESC"
  169.          * })
  170.          */
  171.         private $Children;
  172.         /**
  173.          * @var \Customize\Entity\CategoryTv
  174.          *
  175.          * @ORM\ManyToOne(targetEntity="Customize\Entity\CategoryTv", inversedBy="Children")
  176.          * @ORM\JoinColumns({
  177.          *   @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
  178.          * })
  179.          */
  180.         private $Parent;
  181.         /**
  182.          * @var \Eccube\Entity\Member
  183.          *
  184.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
  185.          * @ORM\JoinColumns({
  186.          *   @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
  187.          * })
  188.          */
  189.         private $Creator;
  190.         /**
  191.          * @var int
  192.          *
  193.          * @ORM\Column(name="no", type="integer", nullable=true)
  194.          */
  195.         private $no;
  196.         /**
  197.          * @var boolean
  198.          *
  199.          * @ORM\Column(name="reserve_flg", type="boolean", options={"default":false})
  200.          */
  201.         private $reserve_flg false;
  202.         /**
  203.          * @var \DateTime
  204.          *
  205.          * @ORM\Column(name="reserve_date", type="datetimetz", nullable=true)
  206.          */
  207.         private $reserve_date;
  208.         /**
  209.          * @var string|null
  210.          *
  211.          * @ORM\Column(name="contents", type="text", nullable=true)
  212.          */
  213.         private $contents;
  214.         /**
  215.          * Constructor
  216.          */
  217.         public function __construct()
  218.         {
  219.             $this->ProductCategoryTvs = new \Doctrine\Common\Collections\ArrayCollection();
  220.             $this->Children = new \Doctrine\Common\Collections\ArrayCollection();
  221.         }
  222.         /**
  223.          * Get id.
  224.          *
  225.          * @return int
  226.          */
  227.         public function getId()
  228.         {
  229.             return $this->id;
  230.         }
  231.         /**
  232.          * Set name.
  233.          *
  234.          * @param string $name
  235.          *
  236.          * @return CategoryTv
  237.          */
  238.         public function setName($name)
  239.         {
  240.             $this->name $name;
  241.             return $this;
  242.         }
  243.         /**
  244.          * Get name.
  245.          *
  246.          * @return string
  247.          */
  248.         public function getName()
  249.         {
  250.             return $this->name;
  251.         }
  252.         /**
  253.          * Set hierarchy.
  254.          *
  255.          * @param int $hierarchy
  256.          *
  257.          * @return CategoryTv
  258.          */
  259.         public function setHierarchy($hierarchy)
  260.         {
  261.             $this->hierarchy $hierarchy;
  262.             return $this;
  263.         }
  264.         /**
  265.          * Get hierarchy.
  266.          *
  267.          * @return int
  268.          */
  269.         public function getHierarchy()
  270.         {
  271.             return $this->hierarchy;
  272.         }
  273.         /**
  274.          * Set sortNo.
  275.          *
  276.          * @param int $sortNo
  277.          *
  278.          * @return CategoryTv
  279.          */
  280.         public function setSortNo($sortNo)
  281.         {
  282.             $this->sort_no $sortNo;
  283.             return $this;
  284.         }
  285.         /**
  286.          * Get sortNo.
  287.          *
  288.          * @return int
  289.          */
  290.         public function getSortNo()
  291.         {
  292.             return $this->sort_no;
  293.         }
  294.         /**
  295.          * Set createDate.
  296.          *
  297.          * @param \DateTime $createDate
  298.          *
  299.          * @return CategoryTv
  300.          */
  301.         public function setCreateDate($createDate)
  302.         {
  303.             $this->create_date $createDate;
  304.             return $this;
  305.         }
  306.         /**
  307.          * Get createDate.
  308.          *
  309.          * @return \DateTime
  310.          */
  311.         public function getCreateDate()
  312.         {
  313.             return $this->create_date;
  314.         }
  315.         /**
  316.          * Set updateDate.
  317.          *
  318.          * @param \DateTime $updateDate
  319.          *
  320.          * @return CategoryTv
  321.          */
  322.         public function setUpdateDate($updateDate)
  323.         {
  324.             $this->update_date $updateDate;
  325.             return $this;
  326.         }
  327.         /**
  328.          * Get updateDate.
  329.          *
  330.          * @return \DateTime
  331.          */
  332.         public function getUpdateDate()
  333.         {
  334.             return $this->update_date;
  335.         }
  336.         /**
  337.          * Add productCategoryTv.
  338.          *
  339.          * @param \Customize\Entity\ProductCategoryTv $productCategoryTv
  340.          *
  341.          * @return Category
  342.          */
  343.         public function addProductCategoryTv(ProductCategoryTv $productCategoryTv)
  344.         {
  345.             $this->ProductCategoryTvs[] = $productCategoryTv;
  346.             return $this;
  347.         }
  348.         /**
  349.          * Remove productCategoryTv.
  350.          *
  351.          * @param \Customize\Entity\ProductCategoryTv $productCategoryTv
  352.          *
  353.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  354.          */
  355.         public function removeProductCategoryTv(ProductCategoryTv $productCategoryTv)
  356.         {
  357.             return $this->ProductCategoryTvs->removeElement($productCategoryTv);
  358.         }
  359.         /**
  360.          * Get productCategoryTvs.
  361.          *
  362.          * @return \Doctrine\Common\Collections\Collection
  363.          */
  364.         public function getProductCategoryTvs()
  365.         {
  366.             return $this->ProductCategoryTvs;
  367.         }
  368.         /**
  369.          * Add child.
  370.          *
  371.          * @param \Customize\Entity\CategoryTv $child
  372.          *
  373.          * @return CategoryTv
  374.          */
  375.         public function addChild(CategoryTv $child)
  376.         {
  377.             $this->Children[] = $child;
  378.             return $this;
  379.         }
  380.         /**
  381.          * Remove child.
  382.          *
  383.          * @param \Customize\Entity\CategoryTv $child
  384.          *
  385.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  386.          */
  387.         public function removeChild(CategoryTv $child)
  388.         {
  389.             return $this->Children->removeElement($child);
  390.         }
  391.         /**
  392.          * Get children.
  393.          *
  394.          * @return \Doctrine\Common\Collections\Collection
  395.          */
  396.         public function getChildren()
  397.         {
  398.             return $this->Children;
  399.         }
  400.         /**
  401.          * Set parent.
  402.          *
  403.          * @param \Customize\Entity\CategoryTv|null $parent
  404.          *
  405.          * @return CategoryTv
  406.          */
  407.         public function setParent(CategoryTv $parent null)
  408.         {
  409.             $this->Parent $parent;
  410.             return $this;
  411.         }
  412.         /**
  413.          * Get parent.
  414.          *
  415.          * @return \Customize\Entity\CategoryTv|null
  416.          */
  417.         public function getParent()
  418.         {
  419.             return $this->Parent;
  420.         }
  421.         /**
  422.          * Set creator.
  423.          *
  424.          * @param \Eccube\Entity\Member|null $creator
  425.          *
  426.          * @return CategoryTv
  427.          */
  428.         public function setCreator(\Eccube\Entity\Member $creator null)
  429.         {
  430.             $this->Creator $creator;
  431.             return $this;
  432.         }
  433.         /**
  434.          * Get creator.
  435.          *
  436.          * @return \Eccube\Entity\Member|null
  437.          */
  438.         public function getCreator()
  439.         {
  440.             return $this->Creator;
  441.         }
  442.         /**
  443.          * Set no.
  444.          *
  445.          * @param int $no
  446.          *
  447.          * @return CategoryTv
  448.          */
  449.         public function setNo($no)
  450.         {
  451.             $this->no $no;
  452.             return $this;
  453.         }
  454.         /**
  455.          * Get no.
  456.          *
  457.          * @return int
  458.          */
  459.         public function getNo()
  460.         {
  461.             return $this->no;
  462.         }
  463.         /**
  464.          * Set reserve_flg.
  465.          *
  466.          * @param boolean $reserve_flg
  467.          *
  468.          * @return CategoryTv
  469.          */
  470.         public function setReserveFlg($reserve_flg)
  471.         {
  472.             $this->reserve_flg $reserve_flg;
  473.             return $this;
  474.         }
  475.         /**
  476.          * Get reserve_flg.
  477.          *
  478.          * @return boolean
  479.          */
  480.         public function isReserveFlg()
  481.         {
  482.             return $this->reserve_flg;
  483.         }
  484.         /**
  485.          * Set reserveDate.
  486.          *
  487.          * @param \DateTime $reserveDate
  488.          *
  489.          * @return CategoryTv
  490.          */
  491.         public function setReserveDate($reserveDate)
  492.         {
  493.             $this->reserve_date $reserveDate;
  494.             return $this;
  495.         }
  496.         /**
  497.          * Get reserveDate.
  498.          *
  499.          * @return \DateTime
  500.          */
  501.         public function getReserveDate()
  502.         {
  503.             return $this->reserve_date;
  504.         }
  505.         /**
  506.          * @return string|null
  507.          */
  508.         public function getContents()
  509.         {
  510.             return $this->contents;
  511.         }
  512.         /**
  513.          * @param string|null $contents
  514.          *
  515.          * @return $this
  516.          */
  517.         public function setContents($contents null)
  518.         {
  519.             $this->contents $contents;
  520.             return $this;
  521.         }
  522.     }
  523. }