I'm pretty new to Doctrine, so any general advice is welcome. I'm trying to achieve the following:
A page can have both videos and photos. Both are media and share properties, so I thought Single Table Inheritance makes sense. Here are the relevant parts of my setup:
Page
/**
* @ORM\Entity
*/
class Page {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Media", mappedBy="page", cascade={"persist"})
* @var ArrayCollection|Media[]
*/
protected $media;
/**
* @return Media[]|ArrayCollection
*/
public function getMedia()
{
return $this->media;
}
}
Media
/**
* @ORM\Entity
* @ORM\Table(name="media")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="media_type", type="string")
* @ORM\DiscriminatorMap({"video" = "Video", "photo" = "Photo"})
*/
abstract class Media {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $url;
/**
* @ORM\ManyToOne(targetEntity="Page", inversedBy="media")
*/
protected $page;
}
Photo
/**
* @ORM\Entity
*/
class Photo extends Media {
/**
* @ORM\Column(type="string")
*/
protected $exif;
}
Video
/**
* @ORM\Entity
*/
class Video extends Media {
/**
* @ORM\Column(type="integer")
*/
protected $length;
}
This works perfectly fine, but -and this is my question- how do I fetch all Photo
s or Video
s of a page. I've tried adding things like
/**
* @ORM\OneToMany(targetEntity="Photo", mappedBy="page", cascade={"persist"})
* @var ArrayCollection|Photo[]
*/
protected $photos;
to Page
, but this results in a schema error, since it doesn't have an inverse defined on Photo. Any help is greatly appreciated!