<?php
namespace App\Repository;
use App\Entity\Artist;
use App\Entity\Event;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Artist|null find($id, $lockMode = null, $lockVersion = null)
* @method Artist|null findOneBy(array $criteria, array $orderBy = null)
* @method Artist[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ArtistRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Artist::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(Artist $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(Artist $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
public function getRandom(int $count = 1, ?Event $event = null)
{
$q = $this->createQueryBuilder('a')
->orderBy('RAND()')
->setMaxResults($count);
if ($event) {
$q->join('a.lineups','l')
->join('l.stage', 's')
->join('s.event','e')
->where('e.id=:event')
->setParameter('event', $event->getId()->toBinary());
}
return $q->getQuery()
->getResult();
}
public function findAll()
{
return $this->findBy([], array('position' => 'ASC'));
}
public function findByEvent(Event $event)
{
return $this->createQueryBuilder('a')
->join('a.lineups','l')
->join('l.stage', 's')
->where('s.event=:event')
->setParameter('event', $event->getId()->toBinary())
->orderBy('a.position', 'ASC')
->getQuery()
->getResult();
}
}