如何在Doctrine 2中管理单表继承?

时间:2011-04-28 19:28:18

标签: php models doctrine-orm single-table-inheritance

我有评论和文章,都是可以投票的。

所以,基本上我有三个实体,ArticleCommentVote

在对Single Table Inheritance in Doctrine2参考手册进行一些阅读后,似乎这就是我需要的内容,因为我的VoteArticleComment上保持不变。

在ORM视图中,以下是我查看Vote表格的方式:

id | resource_id | resource_type | weight |

我想resource_type应该是“鉴别器”列,但我真的不明白如何在我的实体中实现它。

我正在尝试做的是避免必须为我的每个实体投票表,因为投票实体对于两者都保持相同,除了“resource_type”,所以我试图找到一种方法Doctrine2只能使用一个Vote实体。

2 个答案:

答案 0 :(得分:7)

基于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;
}

答案 1 :(得分:2)

只是让其他人需要它,这里是使用表继承与Doctrine的详细示例。我发现它比Doctrine文档更具信息性:

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