Files
ltbxd-actorle/src/Repository/ActorRepository.php
thibaud-leclere f291df0fcf feat: add ActorRepository::findOneRandomInWatchedFilms()
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-01 23:01:57 +02:00

69 lines
1.9 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Actor;
use App\Entity\User;
use App\Entity\UserMovie;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Actor>
*/
class ActorRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Actor::class);
}
public function findOneRandom(?float $popularity = null, ?string $char = null): Actor
{
$qb = $this->createQueryBuilder('o');
$expr = $qb->expr();
if (!empty($popularity)) {
$qb->andWhere($expr->gte('o.popularity', ':popularity'))
->setParameter('popularity', $popularity);
}
if (!empty($char)) {
$qb->andWhere($expr->like('o.name', ':name'))
->setParameter('name', '%'.$char.'%');
}
return $qb
->orderBy('RANDOM()')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findOneRandomInWatchedFilms(User $user, ?float $popularity = null, ?string $char = null): ?Actor
{
$qb = $this->createQueryBuilder('a')
->join('a.movieRoles', 'mr')
->join('mr.movie', 'm')
->join(UserMovie::class, 'um', 'WITH', 'um.movie = m AND um.user = :user')
->setParameter('user', $user);
if (!empty($popularity)) {
$qb->andWhere('a.popularity >= :popularity')
->setParameter('popularity', $popularity);
}
if (!empty($char)) {
$qb->andWhere('LOWER(a.name) LIKE LOWER(:name)')
->setParameter('name', '%' . $char . '%');
}
return $qb
->orderBy('RANDOM()')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
}