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 8788607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:11:09+00:00 2026-06-13T22:11:09+00:00

I am writing a small, forum-like application (in Symfony2) and I am running into

  • 0

I am writing a small, forum-like application (in Symfony2) and I am running into the following problem:

I have two Entities:

class ForumThread {
  public $fourmThreadId;
  public $title;
  public $posts; // aggregation field -> ArrayCollection of ForumPost (can't be empty)
}

class ForumPost {
  public $content;
  public $timeCreated;
  public $thread; // aggregation field -> ForumThread (can't be null)
}

I now want to get all forum threads, including only the newest post of each thread. I want to use it for the forum overview:

-------------------------------------
| Thread                | Last Post |
-------------------------------------
| Name of Thread #1     |    ###    |
| Name of Thread #2     |    ###    |
| ...                   |    ...    |
-------------------------------------

I want to fetch the complete ForumPost entity, not just a single field (like the timestamp).

Also it should be performant: ideally in 1-2 queries (I have found something in raw SQL using INNER JOINs, but I want in DQL or ideally using the QueryBuilder – and my translation tries from SQL to DQL did not succeed).

  • 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-13T22:11:10+00:00Added an answer on June 13, 2026 at 10:11 pm

    Ok, I found a solution, which works but is not be the best way:

    $where = "fp.timeCreated = (SELECT MAX(fpi.timeCreated) FROM ApfelboxGuildBundle:ForumPost fpi WHERE fpi.thread = ft)";
    
    $query = $this->getRepository()->createQueryBuilder("ft")
            ->select("ft, fp")
            ->join("ft.posts", "fp")
            ->groupBy("ft.forumThreadId")
            ->addOrderBy("ft.isSticky", "desc")
            ->addOrderBy("fp.timeCreated", "desc")
            ->addOrderBy("ft.forumThreadId", "desc")
            ->setFirstResult($offset)
            ->setMaxResults(self::THREADS_PER_PAGE);
    

    Notice the subquery in the WHERE clause. This works, but has a flaw: it joins using the timestamp field (to prevent the additional nested subquery – which did always produce parsing errors in DQL), which may not be unique, even for the given thread.

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

Sidebar

Related Questions

I'm writing a small byte array of two bytes using something like the following:
I have an application where I am reading and writing small blocks of data
I'm writing a small ajax class for personal use. In the class, I have
I am writing a small invoice application in Yii . I have the database
I have a small question about a problem I encountered writing my first python
I'm writing a small Windows application in Visual C++ without MVC. Its really small,
I am writing a small e-shop application with Symfony 2 and I need some
I'm writing a small RMI based Chat application. The idea is: the Client registers
I'm writing a small web application using Perl, HTML::Mason and Apache. I've been using
In a small application, I'm writing, during installation phase, the user must enter detail

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.