<?php
namespace Aqarmap\Bundle\FinancialAidsBundle\Repository;
use Aqarmap\Bundle\FinancialAidsBundle\Entity\FinancialAid;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\Persistence\ManagerRegistry;
class FinancialAidRepository extends ServiceEntityRepository
{
public const MAX_RANDOM_RESULT = 1;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, FinancialAid::class);
}
/**
* @return Query
*/
public function getFinancialAids()
{
$queryBuilder = $this->createQueryBuilder('financialAid');
return $queryBuilder->getQuery();
}
/**
* Get random financial Aid.
*
* @return FinancialAid|null
*
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getRandomFinancialAid()
{
return $this->createQueryBuilder('q')
->where('q.status = :status')
->setParameter('status', true)
->addSelect('RAND() as HIDDEN rand')
->addOrderBy('rand')
->setMaxResults(self::MAX_RANDOM_RESULT)
->getQuery()
->getOneOrNullResult();
}
}