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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:33:22+00:00 2026-06-05T18:33:22+00:00

I have a modeling issue with Symfony2/Doctrine2. I am currently trying to pass a

  • 0

I have a modeling issue with Symfony2/Doctrine2. I am currently trying to pass a whole web site from a former PHP framework to Symfony2, and there’s a conception in the former framework which I like but which I can’t manage to modelize with Symfony and Doctrine.

Here is my issue : with the former framework, it’s possible to link an object of a class A to any other object of any other class (B,C,D…). To do this, the class A has an attribute model_id to know which class the object is linked to (it corresponds to a unique id defined in a configuration file for each class), and an attribute record_id which is the external id of the linked object. This way, for example, it’s possible to have a class Comment and have a comment about any object (a blog post, a user, etc.).

As these generic links seem impossible to modelize with Doctrine relationships (ManyToOne etc.), I thought about having – like in the former framework – a sort of global method, or a method for each class, called for example getItem, which looks at the attributes model_id and record_id and then return the right object.

But again I had problems :

  • I can’t define this method in the Entity directly because I shouldn’t have access to the database in the Entity class.

  • If I define it once in a global service, or n times for each class in a repository – which is possible because this time I have access to the database – I will be able to call the method everywhere but in the Entity code. And it makes the implementation pretty ugly because it means that in my previous example all the methods of the Entity which need to have access to the Comments must be moved from the Entity to the Repository. And finally I will have pretty much all my object methods in the Repository rather than in the Entity directly.

Do you have any idea about how I could have a system like this which allows to link generically an object to any other object and then recover easily the linked object just like it was a “usual” Doctrine relationship ?

Thank you very much in advance for your help.

  • 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-05T18:33:23+00:00Added an answer on June 5, 2026 at 6:33 pm

    The thing is your solution means we know in advance that the object A is going to be linked to an object B, C, etc. However, what I wanted to implement is a solution where we don’t know in advance what are going to be the links and we have the possibility to create a new link between the object A and an object of a newly created class Z without changing anything in the classes A and Z. This way, if I take my previous example, you can properly have a Comment on any Object, without making straight relationships between the class Comment and the other classes.

    And I think I found the solution to this problem:

    What I’ve done is implementing a Listener, with the event postLoad which triggers each time an entity is loaded from Doctrine. On the other hand, my entities have for example an attribute $item which is the linked item for which we don’t know the class yet.
    In the postLoad event, we look in the database thanks to model_id and record_id what is the class of the linked item and return the object itself, and then we fill the $item attribute before the entity is returned by Doctrine. This way, each time we get an entity from Doctrine, the “fake” link between the entity and the item is built by the automatic event, and this works for every entity.
    With the same principle, we can update the link with a postUpdate event which triggers each time we update the entity. It will update manually the “fake” link in the database.

    For more information, I recommend to read these pages :

    • http://symfony.com/doc/master/cookbook/doctrine/event_listeners_subscribers.html

    • http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postload

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

Sidebar

Related Questions

I have a typical definition/instance situation in my data modeling. I'm trying to store
I'm trying to use topic modeling with Mallet but have a question. How do
I have a modeling question related to profiles. Firstly, I have looked into using
We have been using Rational Rose 2000 or so for modeling since i guess
I'm modeling an algorithm in Spin. I have a process that has several channels
I am a beginner and have just started experimenting with the modeling tool in
I have a little problem that involves modeling a state machine. I have managed
Let's say I'm modeling phone numbers. I have one entity for PhoneNumber , and
I have terminology/modeling question about representing components in a service. Consider.. Scenario A: ICatalogService
I have some data in csv format I want to use for predictive modeling.

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.