我有评论和文章,都是可选的。
所以,基本上我有三个实体,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;
}
以防有人需要它,这里有一个使用表继承与原则的详细示例。我发现它比Doctrine文档更有信息量:
http://blog.liip.ch/archive/2012/03/27/table-inheritance-with-doctrine.html