» How to Create Dynamic Entity Association with Doctrine2 in Symfony2
How to Create Dynamic Entity Association with Doctrine2 in Symfony2
|April 28, 2014
Posted by forumadmin
I am building a CMS, with dynamic routing. So I have a
Routing entity which stores one or more URLs that point to a
View entity. The
View entity defines basic page information (e.g. title, metatags, etc) as well as what “type” of content this page is, e.g. article, profile, home page, etc. Each of these content types will have different fields and functionality, so they will be defined in unique bundles and they need to exist in their own separate entities (database tables with unique fields). My main Controller finds the
View based on the
Routing url, and loads the right bundle controller to assemble the page (with unique functions and template for the body area).
I would like the View entity to be associated with the specific content type. But traditional ORM mapping won’t work, because the content could be one of several different entities in different bundles, so I can’t map to a single static entity.
I would like to set the association so that I can easily use the formBuilder to create forms that update all the associated entities.
Is there a recommended way to do this?
I read about Resolve Target Entities but you have to register each bundle in the orm config, so I’m not sure if that can handle more than one different bundle.
I also read about Dynamic Form Modification and dynamic mapping via an event subscriber. But I’m also not sure if this is a recommended method for my goals.
Alternatively I just may need to do a OneToOne unidirectional association from the related
Content entity, back to the
View entity, and do a manual call from my main CMS Controller to load the right content entity after I know which content type (bundle and table) and content id (record) the
View refers to. That seems easy enough, but I don’t know whether Symfony/Doctrine has a better solution for dynamic entity mapping. And this method would not automatically work with forms.
For detailed background information about the workflow of the application, you can see a description in my question here, under the heading “Background of Project and Structure of Code”.
More Related Questions
- Symfony2 custom doctrine request I'm still having a problem which let me stop on working because I don't get it solved.
I have 2 tables , Task and TaskDone.
In Task there is stored the ID of the Task ( ID ) and the […]
- Compare values in serialized column in Doctrine with Query Builder I'm building a FormType for a Symfony2 project but I need some Query Builder on the field since I need to compare some values with the one stored on DB and show the results. This is what I […]
- Sylius override product Integrity constraint violation I'm trying to override the product bundle, i did it successfuly but getting an error when trying to delete a parent entity that have products as childs
here's the mapping file […]
- symfony2 error when creating one to one mapping I use FOSUserBundle:
My user entity extends FOS\UserBundle\Model\User:
In my bundle I created user yml file as:
- How to update entities through a relationship with Symfony? I have an article entity and a tag entity in my Symfony application. I have a nested form to edit and tag articles:
When I add a new tag from this form, Doctrine tries to insert a new […]
- How to set up database-heavy unit tests in Symfony2 using PHPUnit? I am quite new to the world of testing and I want to make sure I am on the right track.
I am trying to setup unit tests in a symfony2 project using phpunit.
PHPUnit is functional and the […]
- orm:generate-entities Fails With Namespace I have some YAML mapping files created like so:
- Symfony2/Doctrine2 excessive memory usage while running query This is a separate issue that has developed from [this question thread]
while loading a simple page displaying a table of data (only 2 object entries) I'm getting a memory emergency […]
- Class Project\MyBundle\PhpbbTopics has no association named forumId I'm trying to join the Entities PhpbbTopics and PhpbbForums on the field forumId. These are PhpBB tables and therefore do not have a foreign key on a database level. However, when joining […]
- Accessing Elements In Doctrine Query Not Working I have a Doctrine query, where the "JOIN p.product pr" is joining in the Product Table and all of its info (id, name, qty, status, created, updated, deletedAt, etc...)
Custom function […]