src/Uniski/ConfigBundle/Entity/Category.php line 14

Open in your IDE?
  1. <?php
  2. namespace Uniski\ConfigBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5. /**
  6.  * Category Entity
  7.  *
  8.  * @ORM\Entity
  9.  * @ORM\Table("category")
  10.  */
  11. class Category
  12. {
  13.     /**
  14.      * @var integer
  15.      *
  16.      * @ORM\Id
  17.      * @ORM\Column(name="id", type="integer")
  18.      * @ORM\GeneratedValue(strategy="IDENTITY")
  19.      */
  20.     protected $id;
  21.     /**
  22.      * @var string
  23.      *
  24.      * @ORM\Column(name="name", type="string")
  25.      */
  26.     protected $name;
  27.     /**
  28.      * @var integer
  29.      *
  30.      * @ORM\Column(name="weight", type="integer")
  31.      */
  32.     protected $weight 0;
  33.     /**
  34.      * Parent
  35.      *
  36.      * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  37.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
  38.      */
  39.     protected $parent null;
  40.     /**
  41.      * Children
  42.      *
  43.      * @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"all"})
  44.      * @ORM\OrderBy({"weight" = "asc"})
  45.      */
  46.     protected $children = array();
  47.     /**
  48.      * @Gedmo\Slug(fields={"name"})
  49.      * @ORM\Column(length=128, unique=true)
  50.      */
  51.     private $slug;
  52.     public function setId($id)
  53.      {
  54.          $this->id $id;
  55.          return $this;
  56.      }
  57.      public function getId()
  58.      {
  59.          return $this->id;
  60.      }
  61.      public function setName($name)
  62.      {
  63.          $this->name $name;
  64.          return $this;
  65.      }
  66.      public function getName()
  67.      {
  68.          return $this->name;
  69.      }
  70.      public function setWeight($weight)
  71.      {
  72.          $this->weight $weight;
  73.          return $this;
  74.      }
  75.      public function getWeight()
  76.      {
  77.          return $this->weight;
  78.      }
  79.      public function setParent(Category $category)
  80.      {
  81.          $this->parent $category;
  82.          return $this;
  83.      }
  84.      public function getParent()
  85.      {
  86.          return $this->parent;
  87.      }
  88.      public function addChild(Category $category)
  89.      {
  90.          $this->children[$category->getName()] = $category;
  91.          return $this;
  92.      }
  93.      public function hasChild($name)
  94.      {
  95.          return array_key_exists($name$this->children);
  96.      }
  97.      public function removeChild($name)
  98.      {
  99.          if ($this->hasChild($name)) {
  100.              unset($this->children[$name]);
  101.          }
  102.          return $this;
  103.      }
  104.      public function hasChildren()
  105.      {
  106.          return count($this->getChildren()) ? true false;
  107.      }
  108.      public function getChildren()
  109.      {
  110.          return $this->children;
  111.      }
  112.     public function isRoot()
  113.     {
  114.         return $this->getParent() ? false true;
  115.     }
  116.     public function getRoot()
  117.     {
  118.         $category $this->getParent();
  119.         while ($category && !$category->isRoot()) {
  120.             $category $category->getParent();
  121.         }
  122.         return $category;
  123.     }
  124.     public function getLevel()
  125.     {
  126.         return $this->isRoot() ? $this->getParent()->getLevel() + 1;
  127.     }
  128.     /**
  129.      * Constructor
  130.      */
  131.     public function __construct()
  132.     {
  133.         $this->children = new \Doctrine\Common\Collections\ArrayCollection();
  134.     }
  135.     /**
  136.      * Set slug
  137.      *
  138.      * @param string $slug
  139.      *
  140.      * @return Category
  141.      */
  142.     public function setSlug($slug)
  143.     {
  144.         $this->slug $slug;
  145.         return $this;
  146.     }
  147.     /**
  148.      * Get slug
  149.      *
  150.      * @return string
  151.      */
  152.     public function getSlug()
  153.     {
  154.         return $this->slug;
  155.     }
  156. }