How to manage single table inheritance in Doctrine 2?

I have comments and articles, both are elective.

So basically I have three objects, Article , Comment and Vote .

After some reading in the Single Table Inheritance reference manual in Doctrine2, it seems like this is what I need because my Vote stays the same over Article or Comment .

In the ORM view, this is how I see the Vote table:

id | resource_id | resource_type | weight |

I think resource_type should be a discriminator column, but I really don't understand how to implement this in my essence.

What I'm trying to do is to avoid the voting table for each of my objects, since the voting object remains the same for both, except for "resource_type", so I'm trying to find a way within Doctrine2 to be able to work with one Vote object .

+7
source share
2 answers

Based on an example from docs :

 /** * @Entity * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="resource_type", type="string") * @DiscriminatorMap({"article_vote" = "ArticleVote", "comment_vote" = "CommentVote"}) */ class Vote { private $id; private $weight; } class ArticleVote extends Vote { /** @ManyToOne(...) */ private $article; } class CommentVote extends Vote { /** @ManyToOne(...) */ private $comment; } 
+6
source

Just help someone else need it, here is a detailed example of using Table Inheritance with Doctrine. I found it more informative than the Doctrine documentation:

http://blog.liip.ch/archive/2012/03/27/table-inheritance-with-doctrine.html

+1
source

All Articles