From cf3b357300ab38295612e670b241a64bb86ba537 Mon Sep 17 00:00:00 2001 From: krzysiej Date: Fri, 20 May 2022 15:06:40 +0200 Subject: [PATCH] Added files to books. --- src/Entity/Book.php | 40 +++++++++++++ src/Entity/File.php | 96 +++++++++++++++++++++++++++++++ src/Repository/FileRepository.php | 66 +++++++++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 src/Entity/File.php create mode 100644 src/Repository/FileRepository.php diff --git a/src/Entity/Book.php b/src/Entity/Book.php index b65a22b..b9f9a01 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\BookRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: BookRepository::class)] @@ -37,6 +39,14 @@ class Book #[ORM\Column(type: 'string', length: 255, nullable: true)] private $subtitle; + #[ORM\OneToMany(mappedBy: 'book', targetEntity: File::class, orphanRemoval: true)] + private $files; + + public function __construct() + { + $this->files = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -137,4 +147,34 @@ class Book return $this; } + + /** + * @return Collection + */ + public function getFiles(): Collection + { + return $this->files; + } + + public function addFile(File $file): self + { + if (!$this->files->contains($file)) { + $this->files[] = $file; + $file->setBook($this); + } + + return $this; + } + + public function removeFile(File $file): self + { + if ($this->files->removeElement($file)) { + // set the owning side to null (unless already changed) + if ($file->getBook() === $this) { + $file->setBook(null); + } + } + + return $this; + } } diff --git a/src/Entity/File.php b/src/Entity/File.php new file mode 100644 index 0000000..1cc288e --- /dev/null +++ b/src/Entity/File.php @@ -0,0 +1,96 @@ +id; + } + + public function getFileName(): ?string + { + return $this->file_name; + } + + public function setFileName(string $file_name): self + { + $this->file_name = $file_name; + + return $this; + } + + public function getFileSize(): ?int + { + return $this->file_size; + } + + public function setFileSize(int $file_size): self + { + $this->file_size = $file_size; + + return $this; + } + + public function getExtension(): ?string + { + return $this->extension; + } + + public function setExtension(string $extension): self + { + $this->extension = $extension; + + return $this; + } + + public function getType(): ?string + { + return $this->type; + } + + public function setType(string $type): self + { + $this->type = $type; + + return $this; + } + + public function getBook(): ?Book + { + return $this->book; + } + + public function setBook(?Book $book): self + { + $this->book = $book; + + return $this; + } +} diff --git a/src/Repository/FileRepository.php b/src/Repository/FileRepository.php new file mode 100644 index 0000000..b92edb8 --- /dev/null +++ b/src/Repository/FileRepository.php @@ -0,0 +1,66 @@ + + * + * @method File|null find($id, $lockMode = null, $lockVersion = null) + * @method File|null findOneBy(array $criteria, array $orderBy = null) + * @method File[] findAll() + * @method File[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class FileRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, File::class); + } + + public function add(File $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(File $entity, bool $flush = false): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + +// /** +// * @return File[] Returns an array of File objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('f') +// ->andWhere('f.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('f.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?File +// { +// return $this->createQueryBuilder('f') +// ->andWhere('f.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +}