1

I have an entity Content, with relation to either FileImage or FileVideo. What is the proper way of doing this in Doctrine 2?

Content should relate to FileImage or FileVideo, never both, and never none.

What's a working example to solve the above using annotation syntax?


Or maybe I'm looking at this the wrong way?

My design idea; Files are stored using MogileFS which keeps multiple copies of each file on a subset of servers indexed on filenames. FileVideo contain a list of filenames for different properties (thumbnail, resized etc.) which are different based on file type (E.G FileVideo and FileImage). Paths to the files themselves would be loaded by FileVideo relation to MogileFS mapper outside of Doctrine 2. Business logic should only concern itself with Content model containing ref

Jon Skarpeteig
  • 4,118
  • 7
  • 34
  • 53

1 Answers1

2

Consider some kind of inheritance. FileVideo and FileImage could be subclasses of some parent Entity "File", and your Content entity would have @OneToOne or @ManyToOne relation to "File". You'd be left to your own devices to ensure that Content.file is not null.

Note that there's a tradeoff deciding between class-table and single-table inheritance implementations in this case. CTI will deliver a more normalized schema, and is arguably more flexible if your inheritance graph is likely to grow, but will force doctrine to always eager-load the relation

timdev
  • 61,857
  • 6
  • 82
  • 92
  • How to write annotations for Doctrine to handle non-database existing parent entity? Can it even be done? Having yet another join table for File to reference FileImage and FileVideo doesn't seem like an elegant solution. (E.G why can't this reference logic stay in Content entity?) - Or do I need two columns, and have entity setter to verify that they are mutually exclusive? – Jon Skarpeteig Dec 26 '11 at 22:55
  • File would just be an entity that you don't ever instantiate (so, you'd treat it like it was abstract, even though it won't be). If you're concerned about too many tables/joins, you can just use single-table inheritance. – timdev Dec 27 '11 at 00:52