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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:31:35+00:00 2026-06-13T04:31:35+00:00

I’m having a little problem with a possible multiple relationship in one field in

  • 0

I’m having a little problem with a possible multiple relationship in one field in Doctrine 2. For example, i have an Article entity with one field called author. This field is a ManyToOne to the User entity. However, this field can also relate to the Group entity. So, how the hell can i create such schema?

I have considered creating a new entity, called ArticleAuthor which has two fields: user and group, and depending on form input, i populate one of the fields. This way, this table ArticleAuthor holds it’s own id and the proper relationship to the correct table. Is this a correct approach?

  • 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-13T04:31:36+00:00Added an answer on June 13, 2026 at 4:31 am

    This is what’s referred to as a polymorphic association. Doctrine is able to handle these using Inheritance Mapping

    So you’d define your base entity, such as Author, and then you’d have a GroupAuthor and a UserAuthor which both extend this. These both need to be configured as mapped classes on the base Author entity. It’s up to you whether you opt for single table or class table inheritance; the end result would be the same.

    The last thing to do is to associate the UserAuthor entity to your User entity, and your GroupAuthor to your Group entity.

    Then you’d be able to use it somewhat like this:

    $author = $article->getAuthor();
    if ($author instanceof UserAuthor) {
        $user = $author->getUser();
    } elseif ($author instanceof GroupAuthor) {
        $group = $author->getGroup();
        $users = $group->getUsers(); // Not sure if you'd need this?
    }
    

    Edit: Example mapping

    The ‘parent’ entity, Author.php

    /**
     * @ORM\Entity
     * @ORM\Table(name="authors")
     * @ORM\InheritanceType("JOINED")
     * @ORM\DiscriminatorColumn(name="type", type="string")
     * @ORM\DiscriminatorMap( {"user" = "UserAuthor", "group" = "GroupAuthor"} )
     */
    class Author
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /* any other shared fields... */
    }
    

    Mapped entity, UserAuthor.php

    /**
     * @ORM\Entity
     * @ORM\Table(name="user_authors")
     */
    class UserAuthor extends Author
    {
        /* All columns unique to the UserAuthor entity... */
    }
    

    etc

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.