Multiple level of different types of inheritance
For my project, I am trying to use Doctrine's inheritance feature. I need to represent medias (across different tables: one table for uploaded documents, one for related videos, etc.).
But the video can vary from provider to provider (e.g. Youtube, Dailymotion, you name it). So, I was thinking of another inheritance corresponding to the Video table through inheritance SINGLE_TABLE
.
But, when I declare my entities, it seems that if I add SINGLE_TABLE
an inheritance annotation on an object AbstractVideo
that extends the object AbstractMedia
, the table is Video
never created (and not discovered), Here is a snippet of those two objects:
<?php
namespace Acme\Demo\Entity;
use Datetime;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Media")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
*/
abstract class AbstractMedia
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// some other fields
}
/**
* @ORM\Entity
* @ORM\Table(name="Video")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="provider", type="string")
* @ORM\DiscriminatorMap({})
*/
abstract class AbstractVideo extends AbstractMedia
{
/** @ORM\Column(type="string") */
private $name;
// some other fields
}
I've already tried to bind an object to an object Foo
by extending AbstractVideo
, but then when I try to save something, it says it is not a valid object.
Any ideas or should I really avoid such deep inheritance? Thanks to
source to share
As @OCramius said in a comment on my question , this is not supported by Doctrine ORM. So in order to do what I wanted to do, I would store the value object in a property of data
my object, keeping the property of the "child classes" instead of having a deep different kind of inheritance.
<?php
class Video extends AbstractMedia
{
// returns the value object youtube, dailymotion, ... etc
public function getData();
}
class Youtube
{
//public function ...
}
class Dailymotion
{
// public funciton ...
}
source to share
Not sure if this is exactly what you need, but this is from the production code I am using.
We inherit the file with other objects and they inherit as well. The important part is to add inheriting (extending) objects to the recognition map.
/**
* File
*
* @ORM\Table(name = "file")
* @ORM\Entity(repositoryClass="Living\ApiBundle\Entity\File\FileRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string", length=64)
* @ORM\DiscriminatorMap({
* "file" = "Something\Entity\File\File",
* "image" = "Something\Entity\Image\Image",
* "specialImage" = "Something\Entity\Image\SpecialImage",
* })
*/
class File implements FileEntityInterface
.....
/**
* ImageFile
*
* @ORM\Table(name="image")
* @ORM\Entity(repositoryClass="Living\ApiBundle\Entity\Image\ImageRepository")
*/
class Image extends File implements ImageEntityInterface
source to share