是否可以为 JoinColumn 引用除“id"之外的列?

2023-08-19php开发问题
3

本文介绍了是否可以为 JoinColumn 引用除“id"之外的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 Item 实体,它与 Category 实体具有 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 {
    /**
     * @ORMId
     * @ORMColumn(name = "id", type = "integer")
     * @ORMGeneratedValue(strategy = "AUTO")
     */
    protected $id;
    /**
     * @ORMManyToOne(targetEntity = "Category")
     * @ORMJoinColumn(name = "category_id", referencedColumnName = "id2")
     */
    protected $category;
}

class Category {
    /**
     * @ORMId
     * @ORMColumn(name = "id", type = "integer")
     * @ORMGeneratedValue(strategy = "AUTO")
     */
    protected $id;
    /**
     * @ORMColumn(name = "id2", type = "string", length = "255", unique = "true")
     */
    protected $id2;

当我尝试保存 Item 时,出现此错误:

When I try saving an Item I get this error:

注意:未定义索引:id2 in vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 511

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

果然,如果我在 JoinColumn 注释中将 id2 更改为 id ,一切正常,但我需要连接实体通过 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文档,我想要实现的目标是不可能的.

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

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

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.

来源:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/limitations-and-known-issues.html#join-columns-with-non-primary-keys

推荐答案

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

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.id2type string,我至少希望它是一个整数,但它也可能需要 @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.

您可能只需要 category.id2 上的 @Index 并将其保留为 string;无论如何值得一试.

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"之外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17