学说2多态参考
我有一个“帖子”实体,我希望用户为这些帖子投票。经过身份验证的用户和匿名用户的投票存储在单独的数据库表中,因此有两个单独的“VoteAnonymous”和“VoteAuthenticated”实体实现相同的接口。
现在,我在“Post”实体及其“targetEntity”选项中定义引用时遇到问题。我想知道 Doctrine2 是否有任何方法可以选择多态类之一作为其字段的目标实体。
感谢您的任何帮助。
PS我无法重新设计数据库,这个数据结构上有大量遗留代码。
I have a "Post" entity and I want users to vote for those posts. Votes by authenticated and anonymous users are being stored in separate DB tables, so there are two separate "VoteAnonymous" and "VoteAuthenticated" entities which implement the same interface.
Now I've got problem with defining a reference in a "Post" entity and its "targetEntity" option. I wonder if there is any way Doctrine2 could pick one of the polymorphic classes as its field's target entity.
Thanks for any help.
P.S. I'm not able to redesign the DB, there's a ton of legacy code that lies upon this data structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Doctrine 支持继承,因此您应该创建两个不同实体,它们共享一个共同的父对象,例如定义所有属性的
AbstractVote
。请参阅此答案 - 它包含此类结构的示例。
Doctrine supports inheritance, so you should create two different entities that share a common parrent, say
AbstractVote
, which defines all properties.See this answer - it contains an example of such structure.