【发布时间】:2019-09-09 00:45:21
【问题描述】:
我有一个Item 实体,它与Category 实体有ManyToOne 关系。我希望它们通过类别的id 以外的字段加入(在本例中,是一个名为id2 的字段)。我的架构如下所示。
class Item {
/**
* @ORM\Id
* @ORM\Column(name = "id", type = "integer")
* @ORM\GeneratedValue(strategy = "AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity = "Category")
* @ORM\JoinColumn(name = "category_id", referencedColumnName = "id2")
*/
protected $category;
}
class Category {
/**
* @ORM\Id
* @ORM\Column(name = "id", type = "integer")
* @ORM\GeneratedValue(strategy = "AUTO")
*/
protected $id;
/**
* @ORM\Column(name = "id2", type = "string", length = "255", unique = "true")
*/
protected $id2;
当我尝试保存 Item 时,我收到此错误:
注意:未定义索引:在 vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 行 511 中的 id2
果然,如果我在JoinColumn 注释中将id2 更改为id,一切正常,但我需要通过id2 连接实体。这可能吗?
编辑
根据官方 Doctrine 2 文档,我想要实现的目标是不可能的。
不能使用指向非主键的连接列。 Doctrine 会认为这些是主键并创建延迟加载 代理与数据,这可能会导致意想不到的结果。教义 可以出于性能原因不验证这个的正确性 在运行时设置,但只能通过 Validate Schema 命令。
【问题讨论】:
标签: php doctrine-orm