学说一对一单向 [英] Doctrine one-to-one unidirectional

查看:21
本文介绍了学说一对一单向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有 Doctrine 的 Symfony 3 中,我试图与共享相同主键的两个表建立一对一的单向关系.为此,我试图复制 教义关联映射页面.

In Symfony 3 with Doctrine I'm trying to get a one-to-one unidirectional relationship with both tables sharing the same primary key working. To do so I'm trying to replicate the example on the Doctrine Association Mapping page.

然而,一对一的 uni 文档没有 setter 和 getter 的例子——目标实体上也没有 id 字段的定义.所以我试着围绕自己做实验.

However, the one-to-one uni documentation has no examples of setters and getters - and there is no definition of the id field on the target entity, either. So I tried to experiment around myself.

这些是我的实体:

class Country
{

    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORMOneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
     * @ORMJoinColumn(name="id", referencedColumnName="id", nullable=true)
     */
    private $mysubentity;
    [...]
    /**
     * @return MySubEntity
     */
    public function getMySubEntity()
    {
        return $this->mysubentity;
    }

    /**
     * @param MySubEntity $mysubentity
     */
    public function setMySubEntity($mysubentity)
    {
        $this->mysubentity = $mysubentity;
    }
}

class MySubEntity
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    [..]

    /**
     * Set id
     *
     * @param $id
     *
     * @return MySubEntity
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}

当我保留国家实体时,我收到违反完整性约束:1452 无法添加或更新子行:外键约束失败.在检查数据时,我可以看到 Doctrine 试图将 MySubEntity 的 id 设置为 0.

When I persist the country entity, I receive Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails. When inspecting the data I can see that Doctrine attempted to set the id of MySubEntity to 0.

有人知道我需要做什么才能从 Country 实体自动填充 MySubEntity $id 字段吗?

Does anybody have an idea what I need to do for the MySubEntity $id field to be auto-populated from the Country entity?

推荐答案

您需要将 JoinColumn name 属性更改为除 id 以外的任何其他内容:

You need to change the JoinColumn name property to anything else but not id:

/**
 * @ORMOneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
 * @ORMJoinColumn(name="mysubentity_id", referencedColumnName="id", nullable=true)
 */
private $mysubentity;

这是做什么的:JoinColumn 告诉学说该关系保存在哪个数据库列中.因此,如果您将其称为 mysub_id,则您的主实体将有一个具有该名称的列,其中将保留 referencedColumn 值(您的子实体的 ID).

What does this do: JoinColumn tells doctrine in which database column the relation is saved. so if you call it mysub_id your main entity will have a column with that name in which the referencedColumn value will be persisted (id of your subEntity).

如果您说 JoinColumn 名称是 id,您的实体的主键已经使用了该名称,那么您就会发生冲突.

If you say the JoinColumn name is id which is already used by the primary key of your entity you have a conflict.

我错过了共享相同主键的观点.这有什么具体原因吗?但是如果你真的因为遗留原因需要这样做,要么看看

I missed your point with sharing the same primary key. Is there any specific reason for this? But if you really need to do it for legacy reasons either look at

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-身份

或者可以通过更改生成策略(在这种情况下为 NONE 或自定义)自行生成子实体的主键值

or the possibility to generate the primary key value of your subEntity by yourself by changing the generation strategy (NONE or custom in this case)

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies

这篇关于学说一对一单向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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