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;
...
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.
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.
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.
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:
I accomplished a similar thing using Inheritance defined in traits, which alongside interfaces, basically gave me what a multiple extend would give.
Take a look at embeddables or you could use traits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With