PartCategory.php (2508B)
1 <?php 2 3 namespace PartKeepr\PartBundle\Entity; 4 5 use Doctrine\Common\Collections\ArrayCollection; 6 use Doctrine\ORM\Mapping as ORM; 7 use Gedmo\Mapping\Annotation as Gedmo; 8 use PartKeepr\CategoryBundle\Entity\AbstractCategory; 9 use PartKeepr\CategoryBundle\Entity\CategoryPathInterface; 10 use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService; 11 use Symfony\Component\Serializer\Annotation\Groups; 12 13 /** 14 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository") 15 * @ORM\Table(indexes={@ORM\Index(columns={"lft"}),@ORM\Index(columns={"rgt"})}) 16 * @Gedmo\Tree(type="nested") 17 * The entity for our part categories 18 * 19 * @TargetService(uri="/api/part_categories") 20 */ 21 class PartCategory extends AbstractCategory implements CategoryPathInterface 22 { 23 /** 24 * @Gedmo\TreeParent 25 * @ORM\ManyToOne(targetEntity="PartCategory", inversedBy="children") 26 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE") 27 */ 28 protected $parent; 29 30 /** 31 * @ORM\OneToMany(targetEntity="PartCategory", mappedBy="parent") 32 * @ORM\OrderBy({"lft" = "ASC"}) 33 * @Groups({"tree"}) 34 */ 35 protected $children; 36 37 /** 38 * @ORM\Column(type="text",nullable=true) 39 * @Groups({"default"}) 40 * 41 * @var string 42 */ 43 protected $categoryPath; 44 45 /** 46 * Sets the parent category. 47 * 48 * @Groups({"default"}) 49 * 50 * @param AbstractCategory|null $parent 51 */ 52 public function setParent($parent = null) 53 { 54 $this->parent = $parent; 55 } 56 57 /** 58 * Returns the parent category. 59 * 60 * @return mixed 61 */ 62 public function getParent() 63 { 64 return $this->parent; 65 } 66 67 /** 68 * Returns the children. 69 * 70 * @return ArrayCollection 71 */ 72 public function getChildren() 73 { 74 return $this->children->getValues(); 75 } 76 77 /** 78 * Returns the category path. 79 * 80 * @return string 81 */ 82 public function getCategoryPath() 83 { 84 return $this->categoryPath; 85 } 86 87 /** 88 * {@inheritdoc} 89 */ 90 public function setCategoryPath($categoryPath) 91 { 92 $this->categoryPath = $categoryPath; 93 } 94 95 /** 96 * {@inheritdoc} 97 */ 98 public function generateCategoryPath($pathSeparator) 99 { 100 if ($this->getParent() !== null) { 101 return $this->getParent()->generateCategoryPath($pathSeparator).$pathSeparator.$this->getName(); 102 } else { 103 return $this->getName(); 104 } 105 } 106 }