diff --git a/src/Entity/Rating.php b/src/Entity/Rating.php new file mode 100644 index 0000000..c4642fa --- /dev/null +++ b/src/Entity/Rating.php @@ -0,0 +1,52 @@ +id; + } + + public function getSubjectId(): ?Subject + { + return $this->subject_id; + } + + public function setSubjectId(?Subject $subject_id): static + { + $this->subject_id = $subject_id; + + return $this; + } + + public function getRating(): ?int + { + return $this->rating; + } + + public function setRating(?int $rating): static + { + $this->rating = $rating; + + return $this; + } +} diff --git a/src/Entity/Subject.php b/src/Entity/Subject.php index 4a6b712..560c771 100644 --- a/src/Entity/Subject.php +++ b/src/Entity/Subject.php @@ -4,6 +4,8 @@ namespace App\Entity; use App\Entity\Restaurant; use App\Repository\SubjectRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: SubjectRepository::class)] @@ -24,6 +26,17 @@ abstract class Subject #[ORM\JoinColumn(nullable: true)] private ?User $createdBy = null; + /** + * @var Collection + */ + #[ORM\OneToMany(targetEntity: Rating::class, mappedBy: 'subject_id', orphanRemoval: true)] + private Collection $ratings; + + public function __construct() + { + $this->ratings = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -52,4 +65,34 @@ abstract class Subject return $this; } + + /** + * @return Collection + */ + public function getRatings(): Collection + { + return $this->ratings; + } + + public function addRating(Rating $rating): static + { + if (!$this->ratings->contains($rating)) { + $this->ratings->add($rating); + $rating->setSubjectId($this); + } + + return $this; + } + + public function removeRating(Rating $rating): static + { + if ($this->ratings->removeElement($rating)) { + // set the owning side to null (unless already changed) + if ($rating->getSubjectId() === $this) { + $rating->setSubjectId(null); + } + } + + return $this; + } } diff --git a/src/Factory/RatingFactory.php b/src/Factory/RatingFactory.php new file mode 100644 index 0000000..22f8f43 --- /dev/null +++ b/src/Factory/RatingFactory.php @@ -0,0 +1,53 @@ + + */ +final class RatingFactory extends PersistentObjectFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() {} + + #[\Override] + public static function class(): string + { + return Rating::class; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + #[\Override] + protected function defaults(): array|callable + { + return [ + 'subject_id' => SubjectFactory::new(), + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + #[\Override] + protected function initialize(): static + { + return $this + // ->afterInstantiate(function(Rating $rating): void {}) + ; + } +} diff --git a/src/Repository/RatingRepository.php b/src/Repository/RatingRepository.php new file mode 100644 index 0000000..95754b2 --- /dev/null +++ b/src/Repository/RatingRepository.php @@ -0,0 +1,43 @@ + + */ +class RatingRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Rating::class); + } + + // /** + // * @return Rating[] Returns an array of Rating objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('r') + // ->andWhere('r.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('r.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Rating + // { + // return $this->createQueryBuilder('r') + // ->andWhere('r.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +}