Added files to books.
This commit is contained in:
@@ -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<int, File>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
96
src/Entity/File.php
Normal file
96
src/Entity/File.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\FileRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: FileRepository::class)]
|
||||
class File
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private $id;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private $file_name;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private $file_size;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private $extension;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private $type;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Book::class, inversedBy: 'files')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private $book;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
}
|
||||
66
src/Repository/FileRepository.php
Normal file
66
src/Repository/FileRepository.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\File;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<File>
|
||||
*
|
||||
* @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()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user