Added table for tracking progress of a book.

This commit is contained in:
krzysiej
2022-06-06 15:33:20 +02:00
parent f715f401e1
commit 21743d032c
5 changed files with 216 additions and 9 deletions

View File

@@ -49,20 +49,25 @@ class Book
private $author;
#[ORM\Column(type: 'text', nullable: true)]
private $tags;
private ?string $tags;
#[ORM\Column(type: 'integer', nullable: true)]
private $pages;
private ?int $pages;
#[ORM\Column(type: 'smallint', nullable: true)]
private $rating;
private ?int $rating;
#[ORM\Column(type: 'text', nullable: true)]
private $category;
private ?string $category;
#[ORM\OneToMany(mappedBy: 'book', targetEntity: Progress::class)]
#[ORM\OrderBy(['date' => 'DESC'])]
private $progress;
public function __construct()
{
$this->files = new ArrayCollection();
$this->progress = new ArrayCollection();
}
public function getId(): ?int
@@ -267,4 +272,34 @@ class Book
return $this;
}
/**
* @return Collection<int, Progress>
*/
public function getProgress(): Collection
{
return $this->progress;
}
public function addProgress(Progress $progress): self
{
if (!$this->progress->contains($progress)) {
$this->progress[] = $progress;
$progress->setBook($this);
}
return $this;
}
public function removeProgress(Progress $progress): self
{
if ($this->progress->removeElement($progress)) {
// set the owning side to null (unless already changed)
if ($progress->getBook() === $this) {
$progress->setBook(null);
}
}
return $this;
}
}

66
src/Entity/Progress.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
namespace App\Entity;
use App\Repository\ProgressRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ProgressRepository::class)]
class Progress
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'integer')]
private $pages;
#[ORM\Column(type: 'date')]
private $date;
#[ORM\ManyToOne(targetEntity: Book::class, inversedBy: 'progress')]
#[ORM\JoinColumn(nullable: false)]
private $book;
public function getId(): ?int
{
return $this->id;
}
public function getPages(): ?int
{
return $this->pages;
}
public function setPages(int $pages): self
{
$this->pages = $pages;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getBook(): ?Book
{
return $this->book;
}
public function setBook(?Book $book): self
{
$this->book = $book;
return $this;
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Progress;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Progress>
*
* @method Progress|null find($id, $lockMode = null, $lockVersion = null)
* @method Progress|null findOneBy(array $criteria, array $orderBy = null)
* @method Progress[] findAll()
* @method Progress[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProgressRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Progress::class);
}
public function add(Progress $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Progress $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Progress[] Returns an array of Progress objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('p')
// ->andWhere('p.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('p.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Progress
// {
// return $this->createQueryBuilder('p')
// ->andWhere('p.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}