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

Joh*_*hnT 8 php single-table-inheritance models doctrine-orm

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

所以,基本上我有三个实体Article,CommentVote.

在一些阅读后在Doctrine2单表继承参考手册,似乎它就是我所需要的,因为我Vote仍然是相同的过度ArticleComment.

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

id | resource_id | resource_type | weight |

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

我想要做的是避免必须为我的每个实体投票表,因为投票实体对于两者都保持相同,除了"resource_type",所以我试图在Doctrine2中找到一种方式能够只有一个Vote实体可以使用.

roj*_*oca 7

基于文档中的示例:

/**
 * @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;
}
Run Code Online (Sandbox Code Playgroud)

  • 投票必须是抽象类或鉴别器映射的一部分,才能在继承层次结构中正确映射. (4认同)