Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Doctrine and Symfony to create Polymorphic like associations

I'm attempting to have an Fileable trait that will give provide an Entity with methods to CRUD Files based on the File Entity mentioned below.

After reading the documentation on Doctrine and searching the internet, the best I could find is Inheritance Mapping but these all require the subclass to extend the superclass which is not ideal as the current Entities already extend other classes. I could have FileFoo entity and a FileBar entity but this gets too messy and requires an extra join (super -> sub -> entity).

Alternatively, I could have a File Entity which has many columns for Entities (so foo_id for the Foo object, bar_id for the bar object and so on) but this gets messy and would require a new column for every entity that I'd want to add the Fileable trait too.

So to the questions: Am I thinking about how I want to hold data incorrectly? Is there some features/functions in Doctrine/Symfony that I've missed? Do you think I feature like this would be added if I were to fork Doctrine to add this feature, also where should I look?

<?php
/**
 * File
 *
 * @ORM\Table()
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 */
class File
{
    /**
     * @var integer
     *
     * @ORM\Column(type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     */
    protected $id;
    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    protected $entityName;
    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    protected $entityId;
...
like image 699
TheNextBigThing Avatar asked Apr 26 '16 19:04

TheNextBigThing


People also ask

Are polymorphic associations in relational databases difficult to represent?

Polymorphic associations can be difficult to correctly represent in a relational database. In this post I will compare and contrast four different ways to model these challenging relationships. Imagine you are tasked with designing a fine-grained access control system for a large system.

What is Doctrine ORM in Salesforce?

Doctrine ORM provides a mechanism for transitive persistence through cascading of certain operations. Each association to another entity or a collection of entities can be configured to automatically cascade the following operations to the associated entities: persist, remove, merge, detach, refresh or all.

What is manytoone mapping in doctrine?

This ManyToOne mapping is required. It tells Doctrine to use the category_id column on the product table to relate each record in that table with a record in the category table.

How do I use the category_ID column in doctrine?

It tells Doctrine to use the category_id column on the product table to relate each record in that table with a record in the category table. Next, since one Category object will relate to many Product objects, the make:entity command also added a products property to the Category class that will hold these objects:


2 Answers

I accomplished a similar thing using Inheritance defined in traits, which alongside interfaces, basically gave me what a multiple extend would give.

like image 180
ABM_Dan Avatar answered Sep 29 '22 07:09

ABM_Dan


Take a look at embeddables or you could use traits.

like image 37
d.garanzha Avatar answered Sep 29 '22 07:09

d.garanzha