教义2映射引用唯一键 [英] Doctrine 2 mapping referencing unique key

查看:95
本文介绍了教义2映射引用唯一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本问题:

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

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

扩展版本:

我有一个实体(Participation),它可以引用其他2个实体(DropoutCauseDischargeType).根据此组合,将基于数据库中的另一个(第4个)表(DropoutScenario)隐含一些其他属性.因为两个引用实体中的任何一个都可能为null,所以我无法将它们声明为主键,而只能在第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的缺少值 Application \ Entity \ Trainings \ DropoutScenario

Missing value for primary key id on Application\Entity\Trainings\DropoutScenario

我做错什么了吗,还是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 Application\Entity\Trainings;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\MappedSuperclass
 */
abstract class Participation {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Application\Entity\DropoutCause")
     * @ORM\JoinColumn(name="dropout_cause_id", referencedColumnName="id"))
     */
    protected $dropoutCause;

    /**
     * @ORM\ManyToOne(targetEntity="Application\Entity\DischargeType")
     * @ORM\JoinColumn(name="discharge_id", referencedColumnName="id"))
     */
    protected $dischargeType;

    /**
     * @ORM\ManyToOne(targetEntity="DropoutScenario")
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="discharge_id", referencedColumnName="discharge_id"),
     * @ORM\JoinColumn(name="dropout_cause_id", referencedColumnName="dropout_cause_id")
     * })
     */
    private $scenario;

DropoutScenario:

<?php

namespace Application\Entity\Trainings;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="training_dropout_scenarios")
 */
class DropoutScenario {

    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Application\Entity\DropoutCause")
     * @ORM\JoinColumn(name="dropout_cause_id", referencedColumnName="id"))
     */
    protected $dropoutCause;

    /**
     * @ORM\ManyToOne(targetEntity="Application\Entity\DischargeType")
     * @ORM\JoinColumn(name="discharge_id", referencedColumnName="id"))
     */
    protected $dischargeType;

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

    /** @ORM\Column(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"文档中:

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

不可能使用指向非主键的联接列. 主义会认为这些是主键,并会造成延迟加载 用数据代理,这可能会导致意外结果.教义 出于性能原因可能无法验证此方法的正确性 在运行时进行设置,但只能通过验证模式"命令进行.

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'以外的其他列?

这篇关于教义2映射引用唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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