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

  • SEARCH
  • Home
  • 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 7617191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:05:16+00:00 2026-05-31T03:05:16+00:00

I have three entities: User , Answer , and Question . There is a

  • 0

I have three entities: User, Answer, and Question.

There is a OneToMany relationship between User and Answer and a ManyToOne relationship between Answer and Question. Basically, a particular user optionally gives answers to a bunch of questions.

What I’m trying to accomplish in the world of ORM is retrieving ALL questions and their associated answers for a particular user. The key part is that a user may not have answered a particular question but I still want to get that question (with a null answer).

My Answer entity has a ‘user’ field which maps to the User entity which is inverted by an ‘answers’ field within the User entity. If I use this ‘answers’ field within the User entity, I only get the question/answer pairs for which the user has actually answered. I do not get questions for which the user has not answered.

Typically, using raw SQL, this would involve a simple “left outer join” between my question and answer table, but I want this to be done using Doctrine’s ORM. Any suggestions? I’m pretty new to the world of ORM.

  • 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-05-31T03:05:19+00:00Added an answer on May 31, 2026 at 3:05 am

    I did it! Here’s how:

    I created a field in my Question entity that contains all answers, from all users, for that particular question; its mapped with a OneToMany relationship to the Answer entity. And then to make sure we restrict that list to the answers for a particular user I created a custom repository for my Question entity and created the following function:

    public function findAllJoinedToAnswer($user)
    {
        $query = $this->getEntityManager()
            ->createQuery('
                SELECT q, a
                FROM Bundle:Question q
                LEFT JOIN q.answers a
                WITH a.user = :user'
             )->setParameter('user', $user);
    
        try{
            return $query->getResult();
        }catch (\Doctrine\ORM\NoResultException $e) {
            return null;
        }
    }
    

    Just pass in an instance of the User entity, and voila!

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

Sidebar

Related Questions

I have three entities: User , Team , and TeamInvite . Each User has
I have three entities that must interact: User , SupportTicket and PhoneConversation . When
I have three related entities: a user has many device a user has a
Suppose I have three entities. Department Task User User can publish some Task belonging
I have defined a many-to-many relationship between my two entity classes User and Permission.
I have three entities: User, Office and PhoneNumber. The user has many phone numbers,
I have two entities: Login (user id, password) and Information (title, info). Now there
I have three entities: EntityA, EntityB and EntityC connected with to-many relationships. See schema
I have a case where i have three entities with one-to-many and one-to-many relationships:
So I've got some data. There are entities. Entities have an arbitrary number of

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.