src/Aqarmap/Bundle/FinancialAidsBundle/Repository/FinancialAidRepository.php line 36

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\FinancialAidsBundle\Repository;
  3. use Aqarmap\Bundle\FinancialAidsBundle\Entity\FinancialAid;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\Query;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. class FinancialAidRepository extends ServiceEntityRepository
  8. {
  9.     public const MAX_RANDOM_RESULT 1;
  10.     public function __construct(ManagerRegistry $registry)
  11.     {
  12.         parent::__construct($registryFinancialAid::class);
  13.     }
  14.     /**
  15.      * @return Query
  16.      */
  17.     public function getFinancialAids()
  18.     {
  19.         $queryBuilder $this->createQueryBuilder('financialAid');
  20.         return $queryBuilder->getQuery();
  21.     }
  22.     /**
  23.      * Get random financial Aid.
  24.      *
  25.      * @return FinancialAid|null
  26.      *
  27.      * @throws \Doctrine\ORM\NonUniqueResultException
  28.      */
  29.     public function getRandomFinancialAid()
  30.     {
  31.         return $this->createQueryBuilder('q')
  32.             ->where('q.status = :status')
  33.             ->setParameter('status'true)
  34.             ->addSelect('RAND() as HIDDEN rand')
  35.             ->addOrderBy('rand')
  36.             ->setMaxResults(self::MAX_RANDOM_RESULT)
  37.             ->getQuery()
  38.             ->getOneOrNullResult();
  39.     }
  40. }