Add entity Rating

This commit is contained in:
myrmidex 2026-06-04 19:29:10 +00:00
parent f0d17e9b44
commit 64577e3529
4 changed files with 191 additions and 0 deletions

52
src/Entity/Rating.php Normal file
View file

@ -0,0 +1,52 @@
<?php
namespace App\Entity;
use App\Repository\RatingRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: RatingRepository::class)]
class Rating
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'ratings')]
#[ORM\JoinColumn(nullable: false)]
private ?Subject $subject_id = null;
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
private ?int $rating = null;
public function getId(): ?int
{
return $this->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;
}
}

View file

@ -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<int, Rating>
*/
#[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<int, Rating>
*/
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;
}
}

View file

@ -0,0 +1,53 @@
<?php
namespace App\Factory;
use App\Entity\Rating;
use App\Repository\RatingRepository;
use Doctrine\ORM\EntityRepository;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;
/**
* @extends PersistentObjectFactory<Rating>
*/
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 {})
;
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace App\Repository;
use App\Entity\Rating;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Rating>
*/
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()
// ;
// }
}