Skip to main content

Objects Used in the Examples

src/Entity/Book.php
namespace App\Entity;

use App\Repository\BookRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: BookRepository::class)]
class Book extends \stdClass
{
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME, unique: true, nullable: false)]
private Uuid $id;

#[ORM\Column]
private ?string $title = null;

#[ORM\Column(type: Types::TEXT)]
private ?string $description = null;

#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeInterface $lastChecked = null;

/**
* @var Collection<array-key,Review>
*/
#[ORM\OneToMany(
targetEntity: Review::class,
mappedBy: 'book',
cascade: ['persist', 'remove'],
orphanRemoval: true,
fetch: 'EXTRA_LAZY',
indexBy: 'id',
)]
private Collection $reviews;

public function __construct()
{
$this->id = Uuid::v7();
$this->reviews = new ArrayCollection();
}

/**
* We want to check our books' conditions every now and then.
*/
public function check(): void
{
$this->lastChecked = new \DateTimeImmutable();
}

public function getId(): Uuid
{
return $this->id;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): self
{
$this->title = $title;

return $this;
}

public function getDescription(): ?string
{
return $this->description;
}

public function setDescription(?string $description): self
{
$this->description = $description;

return $this;
}

/**
* @return Collection<array-key,Review>
*/
public function getReviews(): Collection
{
return $this->reviews;
}

public function addReview(Review $review): self
{
if (!$this->reviews->contains($review)) {
$this->reviews[] = $review;
$review->setBook($this);
}

return $this;
}

public function removeReview(Review $review): self
{
if ($this->reviews->removeElement($review)) {
// set the owning side to null (unless already changed)
if ($review->getBook() === $this) {
$review->setBook(null);
}
}

return $this;
}

public function getLastChecked(): ?\DateTimeInterface
{
return $this->lastChecked;
}
}