我有一个与Item
实体有ManyToOne
关系的Category
实体.我想让它们加入除Category之外的字段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;
Run Code Online (Sandbox Code Playgroud)
当我尝试保存时,Item
我收到此错误:
注意:未定义索引:vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php第511行中的id2
果然,如果我改变id2
对id
在JoinColumn
注释,一切工作正常,但我需要的实体通过连接id2
.这可能吗?
根据官方的Doctrine 2文档,编辑我想要实现的目标是不可能的.
无法使用指向非主键的连接列.Doctrine会认为这些是主键并使用数据创建延迟加载代理,这可能会导致意外结果.出于性能原因,Doctrine可以在运行时不验证此设置的正确性,而只能通过Validate Schema命令验证.
来源:http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html