【问题标题】:OneToOne-Relation to two entities implementing an interfaceOneToOne-与实现接口的两个实体的关系
【发布时间】:2013-06-28 11:22:25
【问题描述】:

我在 Doctrine 2.3 中的实体设置有问题(使用 Symfony 2)。

有一个类“Product”,它有一个字段“producer”。 现在这个生产者可以是一个人(“User”类)或一个组(“Group”类)。

我想做的是,现在让 User- 和 Group-class 实现一个接口“Producer”,然后让我的 $producer-Field 映射到 Producer-interface。

/**
 * @var Producer
 * @ORM\OneToOne(targetEntity="..\..\Entity\Producer")
 */
private $producer;

但是,这会失败并显示以下消息:

"Column name `id` referenced for relation from ..\..\Entity\Product towards 
..\..\Entity\Producer does not exist.

再三考虑之后,这似乎很明显 - 但现在我想知道如何解决这个问题。因为如果我不使用任何持久性而只使用 PHP/Java/其他方法,这种方法会起作用 - 但是当映射到数据库时,生活会更加复杂 :-)

继承不是解决方案,因为“用户”已经扩展了“个人”类,并且与“组”没有相似之处,而且 PHP 中没有多重继承之类的东西......

那么,对于这个问题,什么是干净的解决方案?

【问题讨论】:

标签: symfony orm doctrine-orm


【解决方案1】:

http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html

本文档应该对您有所帮助。不确定这是您问题的正确答案

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多