1至1..0的关系,以嵌入的形式与原则 [英] 1 to 1..0 relationship in an embedded form with doctrine
问题描述
一个用户可能只有一个项目或没有。 (1-1..0关系)
One user may have just 1 item or none. (1-1..0 relationship)
我试图在symfony2中用doctrine完成它。
I'm trying to accomplish that in symfony2 with doctrine.
我已经完成了一对一的关系,这是相当简单。但是如何指定教条,当我想创建一个用户,该项目可以为null? (且不要插入新行,只保留id_item为空)
I've accomplished an 1 to 1 relationship, it's fairly simple. But how can I specify to doctrine that when I want to create an user, the item can be null? (and not to insert a new row and just leave id_item null)
这是我所拥有的:
// User Class
/**
*
* @ORM\OneToOne(targetEntity="Items", cascade={"persist"})
* @ORM\JoinColumn(name="id_item", referencedColumnName="id", nullable=true)
*
* @var SOA\AXBundle\Entity\Items $userItem
* @Assert\Type(type="SOA\AXBundle\Entity\Items")
*/
protected $userItem;
当然,我创建了ItemsTypeForm类,并在我的userstypeform类中添加了类型:
And of course, I created ItemsTypeForm class, and added the type in my userstypeform class:
// UsersTypeForm Class
->add('userItem', new \SOA\AXBundle\Form\ItemsTypeForm())
当我添加一个新用户时,一切都很好。插入用户以及项目。但是当我尝试添加没有项目的用户时(用户项目字段为空),我会收到以下错误:
When I add a new user, everything goes fine. The user is inserted as well as the item. But when I try to add an user where it has no item (user item fields are blank), I get the following error:
SQLSTATE [23000]:完整性约束违规:1048列'name'不能为空
正在尝试插入包含空值的项目。
虽然我可以使用一对一的关系,了解如何建立1到1..0的关系。
While I can live with an 1 to 1 relationship, I would like to learn how to make an 1 to 1..0 relationship.
编辑了真正的问题。编辑是粗体。
推荐答案
Assert的注释引起问题。您必须在Assert中设置空值有效。
The annotation of the Assert is causing a problem. You must set that the null value is valid in the Assert.
这篇关于1至1..0的关系,以嵌入的形式与原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!