Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage Single Table Inheritance within Doctrine 2?

I have comments and articles, both are votable.

So, basically I've three entities, Article, Comment and Vote.

After some reading on Single Table Inheritance in Doctrine2 reference manual, it seems that it's what I need, because my Vote remains the same over Article or Comment.

Over the ORM view, here is how I see my Vote table:

id | resource_id | resource_type | weight |

I guess the resource_type should be the "discriminator" column, but I don't really understand how to implement this within my entity.

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

like image 584
JohnT Avatar asked Apr 28 '11 19:04

JohnT


1 Answers

Based on the example from the 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;
}
like image 141
rojoca Avatar answered Oct 07 '22 17:10

rojoca