<?php
namespace App\Repository;
use App\Entity\Beat;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Beat>
*
* @method Beat|null find($id, $lockMode = null, $lockVersion = null)
* @method Beat|null findOneBy(array $criteria, array $orderBy = null)
* @method Beat[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class BeatRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Beat::class);
}
/**
* @return Beat[] Returns an array of Beat objects
*/
public function findAllForHomepage(): array
{
return $this->createQueryBuilder('b')
->orderBy('b.createdAt', 'ASC')
->setMaxResults(6)
->getQuery()
->getResult()
;
}
public function findAllFavoritedBeats($limit): array
{
return $this->createQueryBuilder('b')
->orderBy('b.createdAt', 'ASC')
->setMaxResults(6)
->getQuery()
->getResult()
;
}
public function findAllFavoritedBeatsByAuthor($author,$limit): array
{
return $this->createQueryBuilder('b')
->andWhere('b.author = :author')
->setParameter('author', $author)
//->orderBy('count(b.favoritedClients)', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
public function findAll(array $orderBy = null): array
{
$resQb = $this->createQueryBuilder('b');
if($orderBy != null){
foreach ($orderBy as $order){
$resQb->addOrderBy('b.'.key($orderBy),$order);
}
}else{
$resQb->addOrderBy('b.createdAt', 'ASC');
}
return $resQb
->getQuery()
->getResult()
;
}
public function save(Beat $entity, bool $flush = false): Beat
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
return $entity;
}
public function remove(Beat $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Instrumental[] Returns an array of Instrumental objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('i')
// ->andWhere('i.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('i.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Instrumental
// {
// return $this->createQueryBuilder('i')
// ->andWhere('i.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}