src/Entity/Category.php line 12

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  8. class Category
  9. {
  10.     #[ORM\Id]
  11.     #[ORM\GeneratedValue]
  12.     #[ORM\Column]
  13.     private ?int $id null;
  14.     #[ORM\Column(length255)]
  15.     private ?string $name null;
  16.     #[ORM\ManyToMany(targetEntityBlogPost::class, mappedBy'categories')]
  17.     private Collection $blogPosts;
  18.     public function __construct()
  19.     {
  20.         $this->blogPosts = new ArrayCollection();
  21.     }
  22.     public function getId(): ?int
  23.     {
  24.         return $this->id;
  25.     }
  26.     public function getName(): ?string
  27.     {
  28.         return $this->name;
  29.     }
  30.     public function setName(string $name): static
  31.     {
  32.         $this->name $name;
  33.         return $this;
  34.     }
  35.     /**
  36.      * @return Collection<int, BlogPost>
  37.      */
  38.     public function getBlogPosts(): Collection
  39.     {
  40.         return $this->blogPosts;
  41.     }
  42.     public function addBlogPost(BlogPost $blogPost): self
  43.     {
  44.         if (!$this->blogPosts->contains($blogPost)) {
  45.             $this->blogPosts->add($blogPost);
  46.             $blogPost->addCategory($this);
  47.         }
  48.         return $this;
  49.     }
  50.     public function removeBlogPost(BlogPost $blogPost): self
  51.     {
  52.         if ($this->blogPosts->removeElement($blogPost)) {
  53.             $blogPost->removeCategory($this);
  54.         }
  55.         return $this;
  56.     }
  57. }