简体   繁体   English

symfony2教义关系一对一

[英]symfony2 doctrine relationships one-to-one

I have 2 questions. 我有两个问题。

My first question comes from the doc of doctrine what is "exactly" the difference between the one-to-one unidirectional and the one-to-one bidirectional and when to use either of them? 我的第一个问题来自“学说文档,一对一单向与一对一双向之间的“确切”区别是什么,何时使用它们中的任何一个?

Secondly I have a one-to-one as such 其次,我是一对一的

/**
 * @ORM\OneToOne(targetEntity="App\PurchaseBundle\Entity\Status", cascade={"persist"})
 * @ORM\JoinColumn(nullable=false)
 */
private $status;

When i try to save the form i have an error : 当我尝试保存表单时出现错误:

Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_9861B36D6BF700BD' 

I understood from the doc that I need to do this relationship bidirectional but I dont see why. 我从文档中了解到我需要双向建立这种关系,但我不明白为什么。 All I want is to remove the unique and replace it by index. 我要删除的唯一性,然后将其替换为索引。

Thanks 谢谢

PS : Just in case it might help. PS:以防万一可能会有所帮助。 I have an entity Purchase which at the start will have a status of 'order_created' and will be updated in back office at different process. 我有一个实体Purchase,开始时状态为“ order_created”,并将在后台通过不同的流程进行更新。

To answer at your first question I'll take an example : If you have a User related (with a one to one unidirectionnal relation) to a Car. 为了回答第一个问题,我将举一个例子:如果您有一个与汽车相关的用户(具有一对一的单向关系)。 In this case if you have your user you'll be able to find the car he owns. 在这种情况下,如果您有用户,则可以找到他拥有的汽车。 But if you have the car you won't be able to find the User who owns this car. 但是,如果您有汽车,将无法找到拥有该汽车的用户。 The bidirectionnal permit you to with your object car find your User. 双向允许您与您的目标车一起找到您的用户。

In the first case this code will works : 在第一种情况下,此代码将起作用:

$user->getCar();

But this one won't be possible : 但这不可能:

$car->getUser();

The bidirectionnal allow you to execute both of them. 双向允许您同时执行它们。

Hope this help to understand the concept. 希望这有助于理解概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM