Jon Skarpeteig
Jon Skarpeteig

Reputation: 4128

Doctrine 2 mutually exclusive relations

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

Upvotes: 1

Views: 197

Answers (1)

timdev
timdev

Reputation: 62894

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

Upvotes: 2

Related Questions