原则一对一单向 [英] Doctrine one-to-one unidirectional

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

问题描述

在Symfony 3中与Doctrine我试图与共享相同主键工作的两个表获得一对一的单向关系。为了做到这一点,我试图在教义协会映射页面。



但是,一对一的uni文档没有setter和getter的例子,也没有对目标实体的id字段的定义。所以我试图围绕自己进行实验。



这些是我的实体:

  class Country 
{

/ **
* @var整数
*
* @ ORM\Column(name =id type =integer,precision = 0,scale = 0,nullable = false,unique = false)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY b $ b * /
private $ id;

/ **
* @ ORM\OneToOne(targetEntity =MySubEntity,cascade = {persist,remove})
* @ ORM\JoinColumn (name =id,referencedColumnName =id,nullable = true)
* /
private $ mysubentity;
[...]
/ **
* @return MySubEntity
* /
public function getMySubEntity()
{
return $这 - > mysubentity;
}

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

class MySubEntity
{
/ **
* @var整数
*
* @ ORM\Column(name =id,type =integer,precision = 0,scale = 0,nullable = false,unique = false)
* @ ORM\Id
* ORM\GeneratedValue(strategy =IDENTITY)
* /
private $ id;

[..]

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

return $ this;
}

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

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



有没有人知道我需要为MySubEntity $ id字段做什么要从国家实体自动填充?

解决方案

您需要将JoinColumn名称属性更改为其他任何东西,而不是id:

  / ** 
* @ ORM\OneToOne(targetEntity =MySubEntity,cascade = {persist ,remove})
* @ ORM\JoinColumn(name =mysubentity_id,referencedColumnName =id,nullable = true)
* /
private $ mysubentity;

这样做:
JoinColumn告诉在哪个数据库列保存关系的教条。所以如果你把它称为mysub_id,你的主实体将会有一个列,该列的名称将被persistColumn值保持(你的subEntity的id)。



如果您说的JoinColumn名称是您实体的主键已经使用的id,那么您有冲突。



编辑:



我忘了你的观点,分享相同的主键。有什么具体原因吗?
但是,如果您真的需要为旧的原因,请查看



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



或通过更改生成策略(NONE或在这种情况下自定义)自己生成您的子实体的主键值的可能性



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


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.

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.

These are my entities:

class Country
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
     * @ORM\JoinColumn(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
     *
     * @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(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;
    }
}

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.

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

解决方案

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

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

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).

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

Edit:

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-identity

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天全站免登陆