63 lines
1.3 KiB
PHP
63 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Entity;
|
|
|
|
use App\Repository\CategoryRepository;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
|
|
class Category
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
private ?int $id = null;
|
|
|
|
/**
|
|
* @var Collection<int, Post>
|
|
*/
|
|
#[ORM\OneToMany(targetEntity: Post::class, mappedBy: 'category')]
|
|
private Collection $posts;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->posts = new ArrayCollection();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Post>
|
|
*/
|
|
public function getPosts(): Collection
|
|
{
|
|
return $this->posts;
|
|
}
|
|
|
|
public function addPost(Post $post): static
|
|
{
|
|
if (!$this->posts->contains($post)) {
|
|
$this->posts->add($post);
|
|
$post->setCategory($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removePost(Post $post): static
|
|
{
|
|
if ($this->posts->removeElement($post)) {
|
|
// set the owning side to null (unless already changed)
|
|
if ($post->getCategory() === $this) {
|
|
$post->setCategory(null);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
}
|