Doctrine 2 映射引用唯一键

2022-01-03 00:00:00 php mysql doctrine-orm

基本问题:

是否可以使用 Doctrine 来映射关联而不是主键而是唯一键?

Is it possible to map an association using Doctrine referencing not a primary but only a unique key?

扩展版:

我有一个实体(Participation),它可能引用其他 2 个实体(DropoutCauseDischargeType).根据此组合,基于数据库中的另一个(第四个)表(DropoutScenario),隐含了一些其他属性.因为两个引用的实体中的任何一个都可能为空,所以我无法将它们声明为主键,而只能声明为第 4 个表中的唯一键.

I have an Entity (Participation) which may reference 2 other entities (DropoutCause and DischargeType). Depending on this combination some other attributes are implied, based on another (4th) table (DropoutScenario) in database. Because either of both referenced entities may be null I couldn't declare them as primary but only unique key in the 4th table.

问题是当我尝试用 Doctrine 映射时,我只会收到一个错误:

The problem is I only get an error when I try to map this with Doctrine:

主键 ID 的缺失值ApplicationEntityTrainingsDropoutScenario

Missing value for primary key id on ApplicationEntityTrainingsDropoutScenario

我做错了什么,还是 Doctrine 根本不可能做到这一点?如果没有,有什么更好的解决方案可以做到这一点吗?

Am I doing something wrong, or is this simply not possible with Doctrine? If not, is there any better solution how I could do this?

我已经搜索了很长时间并挖掘了 Doctrine 文档,但我根本找不到关于此的任何内容...

I've been searching for quite a long time now and dug the Doctrine documentation but I simply couldn't find anything on this...

我的映射的剥离代码示例如下.

Stripped code samples of my mappings are below.

参与:

<?php

namespace ApplicationEntityTrainings;

use DoctrineORMMapping as ORM;

/**
 * @ORMMappedSuperclass
 */
abstract class Participation {

    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMManyToOne(targetEntity="ApplicationEntityDropoutCause")
     * @ORMJoinColumn(name="dropout_cause_id", referencedColumnName="id"))
     */
    protected $dropoutCause;

    /**
     * @ORMManyToOne(targetEntity="ApplicationEntityDischargeType")
     * @ORMJoinColumn(name="discharge_id", referencedColumnName="id"))
     */
    protected $dischargeType;

    /**
     * @ORMManyToOne(targetEntity="DropoutScenario")
     * @ORMJoinColumns({
     * @ORMJoinColumn(name="discharge_id", referencedColumnName="discharge_id"),
     * @ORMJoinColumn(name="dropout_cause_id", referencedColumnName="dropout_cause_id")
     * })
     */
    private $scenario;

DropoutScenario:

<?php

namespace ApplicationEntityTrainings;

use DoctrineORMMapping as ORM;

/**
 * @ORMEntity
 * @ORMTable(name="training_dropout_scenarios")
 */
class DropoutScenario {

    /**
     * @var int
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMManyToOne(targetEntity="ApplicationEntityDropoutCause")
     * @ORMJoinColumn(name="dropout_cause_id", referencedColumnName="id"))
     */
    protected $dropoutCause;

    /**
     * @ORMManyToOne(targetEntity="ApplicationEntityDischargeType")
     * @ORMJoinColumn(name="discharge_id", referencedColumnName="id"))
     */
    protected $dischargeType;

    /** @ORMColumn(type="integer", name="dropout_cause_id") */
    protected $dropoutCauseId;

    /** @ORMColumn(type="integer", name="discharge_id") */
    protected $dischargeTypeId;

推荐答案

由于我没有得到任何答案,所以我今天又做了一项研究,并通过 Doctrine 邮件列表论坛找到了我自己问题的答案.好像我刚刚搜索了错误的关键字...

Since I did not get any answer yet I did another research today and I found the answer to my own question through the Doctrine mailing lists forum. Seems like I just searched for the wrong keywords...

不幸的是,Doctrine 2 不支持它.真可惜!:(

来自 Doctrine 文档:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/limitations-and-known-issues.html#join-columns-with-non-主键

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

不能使用指向非主键的连接列.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.

类似问题:是否可以引用一个JoinColumn 的id"以外的列?

相关文章