src/Entity/Category.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\String\Slugger\AsciiSlugger;
  8. /**
  9.  * @ORM\Entity(repositoryClass=CategoryRepository::class)
  10.  */
  11. class Category
  12. {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
  21.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
  22.      */
  23.     private $parent;
  24.     /**
  25.      * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
  26.      */
  27.     private $children;
  28.     /**
  29.      * @ORM\Column(type="string", length=255)
  30.      */
  31.     private $title;
  32.     /**
  33.      * @ORM\Column(type="text", nullable=true)
  34.      */
  35.     private $description;
  36.     /**
  37.      * @ORM\Column(type="string", length=255)
  38.      * @var string
  39.      */
  40.     private $slug;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity=Article::class, mappedBy="category")
  43.      */
  44.     private $articles;
  45.     public function __construct()
  46.     {
  47.         $this->articles = new ArrayCollection();
  48.     }
  49.     public function getId(): ?int
  50.     {
  51.         return $this->id;
  52.     }
  53.     public function getTitle(): ?string
  54.     {
  55.         return $this->title;
  56.     }
  57.     public function setTitle(string $title): self
  58.     {
  59.         $this->title $title;
  60.         return $this;
  61.     }
  62.     public function getDescription(): ?string
  63.     {
  64.         return $this->description;
  65.     }
  66.     public function setDescription(?string $description): self
  67.     {
  68.         $this->description $description;
  69.         return $this;
  70.     }
  71.     /**
  72.      * @return Collection|Article[]
  73.      */
  74.     public function getArticles(): Collection
  75.     {
  76.         return $this->articles;
  77.     }
  78.     public function addArticle(Article $article): self
  79.     {
  80.         if (!$this->articles->contains($article)) {
  81.             $this->articles[] = $article;
  82.             $article->setCategory($this);
  83.         }
  84.         return $this;
  85.     }
  86.     public function removeArticle(Article $article): self
  87.     {
  88.         if ($this->articles->contains($article)) {
  89.             $this->articles->removeElement($article);
  90.             // set the owning side to null (unless already changed)
  91.             if ($article->getCategory() === $this) {
  92.                 $article->setCategory(null);
  93.             }
  94.         }
  95.         return $this;
  96.     }
  97.     /**
  98.      *
  99.      * @return  string
  100.      */
  101.     public function getSlug(): ?string
  102.     {
  103.         return $this->slug;
  104.     }
  105.     /**
  106.      *
  107.      * @param  string  $slug
  108.      *
  109.      * @return  self
  110.      */
  111.     public function setSlug(string $slug): self
  112.     {
  113.         $slugger = new AsciiSlugger('fr');
  114.         $this->slug $slugger->slug(strtolower($slug));
  115.         return $this;
  116.     }
  117.     public function getParent(): ?Category
  118.     {
  119.         return $this->parent;
  120.     }
  121.     public function setParent(?Category $parent): self
  122.     {
  123.         $this->parent $parent;
  124.         return $this;
  125.     }
  126.     public function getChildren(): Collection
  127.     {
  128.         return $this->children;
  129.     }
  130.     public function addChild(Category $child): self
  131.     {
  132.         if (!$this->children->contains($child)) {
  133.             $this->children[] = $child;
  134.             $child->setParent($this);
  135.         }
  136.         return $this;
  137.     }
  138.     public function removeChild(Category $child): self
  139.     {
  140.         if ($this->children->contains($child)) {
  141.             $this->children->removeElement($child);
  142.             if ($child->getParent() === $this) {
  143.                 $child->setParent(null);
  144.             }
  145.         }
  146.         return $this;
  147.     }
  148. }