59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\Answer;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<Answer>
|
|
*/
|
|
class AnswerRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, Answer::class);
|
|
}
|
|
|
|
public function findAnswersByQuestion(int $questionId, int $limit = 20, int $offset = 0): array
|
|
{
|
|
return $this->createQueryBuilder('a')
|
|
->where('a.question = :questionId')
|
|
->andWhere('a.isActive = :active')
|
|
->setParameter('questionId', $questionId)
|
|
->setParameter('active', true)
|
|
->orderBy('a.isAccepted', 'DESC')
|
|
->addOrderBy('a.votes', 'DESC')
|
|
->addOrderBy('a.createdAt', 'ASC')
|
|
->setMaxResults($limit)
|
|
->setFirstResult($offset)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
public function countAnswersByQuestion(int $questionId): int
|
|
{
|
|
return $this->createQueryBuilder('a')
|
|
->select('COUNT(a.id)')
|
|
->where('a.question = :questionId')
|
|
->andWhere('a.isActive = :active')
|
|
->setParameter('questionId', $questionId)
|
|
->setParameter('active', true)
|
|
->getQuery()
|
|
->getSingleScalarResult();
|
|
}
|
|
|
|
public function findMostVotedAnswers(int $limit = 10): array
|
|
{
|
|
return $this->createQueryBuilder('a')
|
|
->where('a.isActive = :active')
|
|
->setParameter('active', true)
|
|
->orderBy('a.votes', 'DESC')
|
|
->addOrderBy('a.createdAt', 'DESC')
|
|
->setMaxResults($limit)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
}
|