Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:36:05+00:00 2026-06-07T15:36:05+00:00

I’m trying to retrieve comments from a table Comment which has id, game (a

  • 0

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 ‘Doctrine\ORM\NoResultException’ 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:36:08+00:00Added an answer on June 7, 2026 at 3:36 pm

    If you will only be using MySQL, then you can take advantage of its FOUND_ROWS() function.

    This will require using native queries, which will most likely hinder your ability to use a DB other than MySQL, but it works quite well in my experience.

    I have used something like the following with great success.

    use Doctrine\ORM\Query\ResultSetMapping;
    
    public function getRecentComments($offset, $id) {
        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM Comment c 
            WHERE c.game = ?
            ORDER BY c.date DESC
            LIMIT ?,3";
        $rsm = new ResultSetMapping();
        $rsm->addEntityResult('Comment', 'c');
        $rsm->addFieldResult('c', 'id', 'id');
        $rsm->addFieldResult('c', 'game_id', 'game_id');
        $rsm->addFieldResult('c', 'date', 'date');
        $query = $this->getEntityManager()->createNativeQuery($dql, $rsm);
        $query->setParameters(array(
          (int)$id,
          (int)$offset
        ));
        $results = $query->getResult();
    
        // Run FOUND_ROWS query and add to results array
        $sql = 'SELECT FOUND_ROWS() AS foundRows';
        $rsm = new ResultSetMapping();
        $rsm->addScalarResult('foundRows', 'foundRows');
        $query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
        $foundRows = $query->getResult();
        $results['foundRows'] = $foundRows[0]['foundRows'];
    
        return $results;
    }
    

    After getting the results array from the above function, I extract the ‘foundRows’ element to a separate variable, unset it (i.e., unset($results['foundRows'])), and then continue using the array as normal.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.