流向双向一对一关系 [英] Bidirectional One-To-One Relationships in Flow

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

问题描述

在流量中是否有一对一关系,而不必设置属性两次

Is it possible to have One-To-One Relationships in Flow without having to set the attributes twice?

我有两张以一对一关系连接的表格,但只有其中一个表格应包含此关系的额外列。

I have two tables that are connected in a One-To-One Relationship, but only one of them should contain an extra column for this Relation.

明确的原则支持这种行为:
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-one-bidirectional

Doctrine clearly supports this behavior: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-one-bidirectional

应该带有一个componenttape列的类:

The class that should come with a componenttape column:

/**
 * @Flow\Entity
*/
class Component{
    /**
     * @var \Some\Package\Domain\Model\Component\Tape
     * @ORM\OneToOne(cascade={"all"}, inversedBy="component")
     */
    protected $componentTape;
   …
}

应该只能找到连接的类没有额外的列:

The class that should just be able to find the connection without an extra column:

/**
 * @Flow\Entity
*/
class Tape{
    /**
     * @var \ Some\Package\Domain\Model\Component
     * @ORM\OneToOne(mappedBy="componentTape")
     */
    protected $component;
}

一个学说更新将为这两个模型创建额外的列。

A doctrine update will create extra columns for both models.

这是我现在的工作环境如下所示:

This is what my workarround at the moment looks like:

class Component{    
     ..    
     /**
     * @param \Some\Package\Domain\Model\Component\Tape $componentTape
     * @return void
     */
    public function setComponentTape($componentTape) {
        $this->componentTape = $componentTape;
        $this->componentTape->setComponent($this);
    }


推荐答案

解决方法无论如何也是必要的在请求期间始终保持关系正确。
但是第二个DB列不应该是必需的。你检查了教义是否真的填补了?也许/可能只是创建的迁移是错误的,磁带中的组件列可以省略。

The workaround will be necessary anyway to keep the relation correct at all times during a request. But the second DB column shouldn't be necessary. Did you check if doctrine actually fills it? Maybe/Probably just the created migration is wrong and the component column in Tape can be omitted.

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

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