Joh*_*hnT 8 php single-table-inheritance models doctrine-orm
我有评论和文章,都是可以投票的.
所以,基本上我有三个实体Article,Comment和Vote.
在一些阅读后在Doctrine2单表继承参考手册,似乎它就是我所需要的,因为我Vote仍然是相同的过度Article或Comment.
在ORM视图中,以下是我查看Vote表格的方式:
id | resource_id | resource_type | weight |
我想resource_type应该是"鉴别器"列,但我真的不明白如何在我的实体中实现它.
我想要做的是避免必须为我的每个实体投票表,因为投票实体对于两者都保持相同,除了"resource_type",所以我试图在Doctrine2中找到一种方式能够只有一个Vote实体可以使用.
基于文档中的示例:
/**
* @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)