对于JoinColumn,是否可以引用'id'以外的列? [英] Is it possible to reference a column other than 'id' for a JoinColumn?

查看:665
本文介绍了对于JoinColumn,是否可以引用'id'以外的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目实体,其 ManyToOne 类别实体。我希望他们被除了类别 id 之外的字段加入(在这种情况下,称为 id2 的字段)。我的模式如下所示。

I have an Item entity that has a ManyToOne relationship to a Category entity. I want them to be joined by a field other than Category's id (in this case, a field called id2). My schema is listed below.

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;

当我尝试保存项目这个错误:

When I try saving an Item I get this error:


注意:未定义的索引:vendor / doctrine / lib / Doctrine / ORM / Persisters / BasicEntityPersister.php行中的id2第511行

Notice: Undefined index: id2 in vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 511

果然,如果我将 id2 更改为 id JoinColumn 注释中,一切正常,但我需要通过 id2 。这是可能吗?

Sure enough, if I change id2 to id in the JoinColumn annotation, everything works fine, but I need the entities to be connected through id2. Is this possible?

编辑

根据官方的Doctrine 2文档,我想要实现的是不可能的。 / p>

Edit
What I want to achieve is impossible according to the official Doctrine 2 docs.


无法使用指向非主键的连接列。
学说会认为这些是主要的关键,并创建延迟加载
代理与数据,这可能会导致意想不到的结果。 Doctrine
可能出于性能原因,无法在运行时验证此
设置的正确性,但只能通过验证模式命令。

It is not possible to use join columns pointing to non-primary keys. Doctrine will think these are the primary keys and create lazy-loading proxies with the data, which can lead to unexpected results. Doctrine can for performance reasons not validate the correctness of this settings at runtime but only through the Validate Schema command.



来源: http: //www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html

推荐答案

我认为教义希望这些是主要的关键,从文档

I think Doctrine wants these to be primary keys, from the docs:


name:保存此关系的外键标识符的列名称。

name: Column name that holds the foreign key identifier for this relation.

另外从代码示例中跳出来的东西是 category.id2 string ,我至少会希望它是一个整数,但它可以是o需要为 @JoinColumn 正常工作。

Another thing that jumps out at me from your code sample is category.id2 being type string, I would at least expect it to be an integer, but it may also need to be for @JoinColumn to work properly.

您可以使用@Index category.id2 上,并留下作为字符串虽然;值得一击。

You may be able to get away with just @Index on category.id2 and leave it as a string though; worth a shot anyway.

这篇关于对于JoinColumn,是否可以引用'id'以外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆