I'm trying to retrieve comments from a table Comment which has id, game (a foreign key) and date.
Every time I ask for comments I want to get 3 comments sorted by date for a specified game and I want to know if there is more comments to show later. For that, I've written two functions, the first one returns the three comments:
public function getRecentComments($offset,$id) {
$dql = "SELECT c FROM Comment c
WHERE c.game = ?1
ORDER BY c.date DESC";
$query = $this->getEntityManager()->
createQuery($dql)->
setParameter(1, (int)$id)->
setMaxResults(3)->
setFirstResult($offset);
return $query->getResult();
And the second one returns the number of comments I could get later. The reason of this function is wehter show a button "More comments" or not. This is the second function:
public function moreComments($offset,$id) {
$dql = "SELECT COUNT(c.id) FROM Comment c
WHERE c.game = ?1
ORDER BY c.date DESC";
$query = $this->getEntityManager()
->createQuery($dql)
->setParameter(1, (int)$idPartido)
->setFirstResult($offset+3)
->setMaxResults(1)
->getSingleScalarResult();
return $query;
}
But the second function doesn't work for the next error:
Fatal error: Uncaught exception 'DoctrineORMNoResultException' with message 'No result was found for query although at least one row was expected.
Which I think it is due to use setFirstResult and count().
So, I've used
public function moreComments($offset,$id) {
$dql = "SELECT c FROM Comentario c
WHERE c.partido = ?1
ORDER BY c.fecha DESC";
$query = $this->getEntityManager()
->createQuery($dql)
->setParameter(1, (int)$idPartido)
->setFirstResult($offset+3)
->setMaxResults(1)
->getSingleScalarResult();
return sizeof($query);
}
Which obviously is bad written because I shouldn't get the data for only a count. How could I write the second function correctly?
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…