src/Entity/Category.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use Cocur\Slugify\Slugify;
  5. use App\Repository\CategoryRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. /**
  12.  * @ORM\Entity(repositoryClass=CategoryRepository::class)
  13.  * @UniqueEntity(
  14.  *  fields="title",
  15.  *  message="Cette catégorie existe ! "
  16.  * )
  17.  * @ApiResource()
  18.  */
  19. class Category
  20. {
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\Column(type="string", length=255)
  29.      * @Assert\Length(
  30.      *     min = 3,
  31.      *     max = 70
  32.      * )
  33.      * @Assert\NotBlank()
  34.      */
  35.     private $title;
  36.     /**
  37.      * @ORM\OneToMany(targetEntity=Article::class, mappedBy="category", orphanRemoval=true)
  38.      */
  39.     private $articles;
  40.     /**
  41.      * @ORM\Column(type="boolean")
  42.      */
  43.     private $is_active;
  44.     /**
  45.      * @ORM\ManyToOne(targetEntity=Category2::class, inversedBy="categorys")
  46.      */
  47.     private $category2;
  48.     /**
  49.      * @ORM\Column(type="string", length=255)
  50.      */
  51.     private $slug;
  52.     public function __construct()
  53.     {
  54.         $this->articles = new ArrayCollection();
  55.     }
  56.     public function getId(): ?int
  57.     {
  58.         return $this->id;
  59.     }
  60.     public function getTitle(): ?string
  61.     {
  62.         return $this->title;
  63.     }
  64.     public function getSlug()
  65.     {   
  66.         return $this->slug;
  67.     }
  68.     public function setSlug(string $slug): self
  69.     {
  70.         $Slugify = new Slugify();
  71.         $slug  $Slugify->slugify($this->getTitle());
  72.         $this->slug $slug;
  73.         return $this;
  74.     }
  75.     public function setTitle(string $title): self
  76.     {
  77.         $this->title ucfirst($title);
  78.         return $this;
  79.     }
  80.     /**
  81.      * @return Collection|Article[]
  82.      */
  83.     public function getArticles(): Collection
  84.     {
  85.         return $this->articles;
  86.     }
  87.     public function addArticle(Article $article): self
  88.     {
  89.         if (!$this->articles->contains($article)) {
  90.             $this->articles[] = $article;
  91.             $article->setCategory($this);
  92.         }
  93.         return $this;
  94.     }
  95.     public function removeArticle(Article $article): self
  96.     {
  97.         if ($this->articles->removeElement($article)) {
  98.             // set the owning side to null (unless already changed)
  99.             if ($article->getCategory() === $this) {
  100.                 $article->setCategory(null);
  101.             }
  102.         }
  103.         return $this;
  104.     }
  105.     public function getIsActive(): ?bool
  106.     {
  107.         return $this->is_active;
  108.     }
  109.     public function setIsActive(bool $is_active): self
  110.     {
  111.         $this->is_active $is_active;
  112.         return $this;
  113.     }
  114.     public function getCategory2(): ?Category2
  115.     {
  116.         return $this->category2;
  117.     }
  118.     public function setCategory2(?Category2 $category2): self
  119.     {
  120.         $this->category2 $category2;
  121.         return $this;
  122.     }
  123. }