Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic association to different owner objects in Doctrine 2

is it possible to use kind of polymorphic association to different owner objects in Doctrine 2? I mean that for example ImageGroup entity is owned by different entities: Article, Album, Post, Comment, etc. So there should be "ownerId" and "ownerType" attributes in ImageGroup table, where "ownerType" is discriminator.

I am already using Class Table Inheritance and also Superclass mapping on other object relationships in my project, but any of them suits for this problem, if I am correct. Because in these situations referred objects are inherited.

I have read answers to polymorphic associations here, but I found no answer to my example.

In my example is no inheritance at all. Any help, please? Thanks in advance!

like image 982
Martin Avatar asked Dec 07 '12 13:12

Martin


1 Answers

What you are looking for is the strategy pattern, which basically allows you to have a weak link between your objects and the ones you want to reference, such as ['name' => 'EntityName', 'identifier' => 123].

like image 98
Ocramius Avatar answered Nov 10 '22 03:11

Ocramius