app/Plugin/Blogs42/Entity/Category.php line 18

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