<?php
namespace App\Entity;
use App\Repository\BlogRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Traits\Timestampable;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\HasLifecycleCallbacks]
#[Vich\Uploadable]
#[ORM\Table(name: "blog")]
#[ORM\Index(columns: ["title","content"], name: "blog",flags: ['fulltext'])]
#[ORM\Entity(repositoryClass: BlogRepository::class)]
class Blog
{
use Timestampable;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $content = null;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
*/
#[Vich\UploadableField(mapping: 'blog_image', fileNameProperty: 'imageName')]
private ?File $imageFile = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $imageName = null;
#[ORM\OneToMany(mappedBy: 'blog', targetEntity: Comment::class)]
private Collection $comments;
#[ORM\Column(length: 255)]
private ?string $slug = null;
#[ORM\ManyToOne(inversedBy: 'blogs', cascade: ["persist"])]
#[ORM\JoinColumn(nullable: false)]
private ?BlogCategory $category = null;
public function __construct()
{
$this->comments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): static
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): static
{
$this->content = $content;
return $this;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
*/
public function setImageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
if (null !== $imageFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->setUpdatedAt(new \DateTime());
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageName(?string $imageName): static
{
$this->imageName = $imageName;
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setBlog($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getBlog() === $this) {
$comment->setBlog(null);
}
}
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): static
{
$this->slug = $slug;
return $this;
}
public function getCategory(): ?BlogCategory
{
return $this->category;
}
public function setCategory(?BlogCategory $category): static
{
$this->category = $category;
return $this;
}
}