src/Repository/BeatRepository.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Beat;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * @extends ServiceEntityRepository<Beat>
  8.  *
  9.  * @method Beat|null find($id, $lockMode = null, $lockVersion = null)
  10.  * @method Beat|null findOneBy(array $criteria, array $orderBy = null)
  11.  * @method Beat[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  12.  */
  13. class BeatRepository extends ServiceEntityRepository
  14. {
  15.     public function __construct(ManagerRegistry $registry)
  16.     {
  17.         parent::__construct($registryBeat::class);
  18.     }
  19.     /**
  20.      * @return Beat[] Returns an array of Beat objects
  21.      */
  22.     public function findAllForHomepage(): array
  23.     {
  24.         return $this->createQueryBuilder('b')
  25.             ->orderBy('b.createdAt''ASC')
  26.             ->setMaxResults(6)
  27.             ->getQuery()
  28.             ->getResult()
  29.             ;
  30.     }
  31.     public function findAllFavoritedBeats($limit): array
  32.     {
  33.         return $this->createQueryBuilder('b')
  34.             ->leftJoin('b.favoritedClients''fc')
  35.             ->groupBy('b.id')
  36.             ->orderBy('COUNT(fc.id)''DESC')
  37.             ->setMaxResults($limit)
  38.             ->getQuery()
  39.             ->getResult();
  40.     }
  41.     public function findAllFavoritedBeatsByAuthor($author$limit): array
  42.     {
  43.         return $this->createQueryBuilder('b')
  44.             ->leftJoin('b.favoritedClients''fc')
  45.             ->andWhere('b.author = :author')
  46.             ->setParameter('author'$author)
  47.             ->groupBy('b.id')
  48.             ->orderBy('COUNT(fc.id)''DESC')
  49.             ->setMaxResults($limit)
  50.             ->getQuery()
  51.             ->getResult();
  52.     }
  53. //    public function findAllFavoritedBeatsByAuthor($author,$limit): array
  54. //    {
  55. //        return $this->createQueryBuilder('b')
  56. //            ->andWhere('b.author = :author')
  57. //            ->setParameter('author', $author)
  58. //            ->orderBy('count(b.favoritedClients)', 'DESC')
  59. //            ->setMaxResults($limit)
  60. //            ->getQuery()
  61. //            ->getResult()
  62. //            ;
  63. //
  64. //    }
  65.     public function findAll(array $orderBy null): array
  66.     {
  67.         $resQb $this->createQueryBuilder('b');
  68.         if($orderBy != null){
  69.             foreach ($orderBy as $order){
  70.                 $resQb->addOrderBy('b.'.key($orderBy),$order);
  71.             }
  72.         }else{
  73.             $resQb->addOrderBy('b.createdAt''ASC');
  74.         }
  75.         return $resQb
  76.             ->getQuery()
  77.             ->getResult()
  78.             ;
  79.     }
  80.     public function save(Beat $entitybool $flush false): Beat
  81.     {
  82.         $this->getEntityManager()->persist($entity);
  83.         if ($flush) {
  84.             $this->getEntityManager()->flush();
  85.         }
  86.         return $entity;
  87.     }
  88.     public function remove(Beat $entitybool $flush false): void
  89.     {
  90.         $this->getEntityManager()->remove($entity);
  91.         if ($flush) {
  92.             $this->getEntityManager()->flush();
  93.         }
  94.     }
  95. //    /**
  96. //     * @return Instrumental[] Returns an array of Instrumental objects
  97. //     */
  98. //    public function findByExampleField($value): array
  99. //    {
  100. //        return $this->createQueryBuilder('i')
  101. //            ->andWhere('i.exampleField = :val')
  102. //            ->setParameter('val', $value)
  103. //            ->orderBy('i.id', 'ASC')
  104. //            ->setMaxResults(10)
  105. //            ->getQuery()
  106. //            ->getResult()
  107. //        ;
  108. //    }
  109. //    public function findOneBySomeField($value): ?Instrumental
  110. //    {
  111. //        return $this->createQueryBuilder('i')
  112. //            ->andWhere('i.exampleField = :val')
  113. //            ->setParameter('val', $value)
  114. //            ->getQuery()
  115. //            ->getOneOrNullResult()
  116. //        ;
  117. //    }
  118. }