是否可以为JoinColumn引用“id”以外的列?

时间:2022-12-06 10:26:47

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.

我有一个Item实体,它与Category实体有一个ManyToOne关系。我希望它们由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;

When I try saving an Item I get this error:

当我尝试保存项目时,我收到此错误:

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

注意:未定义索引:vendor / doctrine / lib / Doctrine / ORM / Persisters / BasicEntityPersister.php第511行中的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?

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

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

编辑根据官方的Doctrine 2文档,我想要实现的目标是不可能的。

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.

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

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

2 个解决方案

#1


7  

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

我认为Doctrine希望这些是主键,来自文档:

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

name:包含此关系的外键标识符的列名。

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.

从你的代码示例中跳出来的另一件事是category.id2是类型字符串,我至少期望它是一个整数,但它也可能需要@JoinColumn才能正常工作。

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

你可以在category.id2上只使用@Index,然后将其保留为字符串;无论如何值得一试。

#2


3  

Just to report. I was able to join non-PKs in Many2One (undirectional) relation, BUT my object can't be loaded the normal way. It must be loaded with DQL like:

只是为了报道。我能够在Many2One(非定向)关系中加入非PK,但是我的对象无法以正常方式加载。它必须加载DQL,如:

SELECT d,u FROM DEntity d
    JOIN d.userAccount u

this way I stopped getting error: Missing value for primary key id on ....

这样我就不再收到错误:主键ID的值缺失....

#1


7  

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

我认为Doctrine希望这些是主键,来自文档:

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

name:包含此关系的外键标识符的列名。

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.

从你的代码示例中跳出来的另一件事是category.id2是类型字符串,我至少期望它是一个整数,但它也可能需要@JoinColumn才能正常工作。

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

你可以在category.id2上只使用@Index,然后将其保留为字符串;无论如何值得一试。

#2


3  

Just to report. I was able to join non-PKs in Many2One (undirectional) relation, BUT my object can't be loaded the normal way. It must be loaded with DQL like:

只是为了报道。我能够在Many2One(非定向)关系中加入非PK,但是我的对象无法以正常方式加载。它必须加载DQL,如:

SELECT d,u FROM DEntity d
    JOIN d.userAccount u

this way I stopped getting error: Missing value for primary key id on ....

这样我就不再收到错误:主键ID的值缺失....