One user may have just 1 item or none. (1-1..0 relationship)
I'm trying to accomplish that in symfony2 with doctrine.
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)
This is what I have:
// 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;
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]: Integrity constraint violation: 1048 Column 'name' cannot be null
It is trying to insert the item, with null values.
While I can live with an 1 to 1 relationship, I would like to learn how to make an 1 to 1..0 relationship.
edited with the real problem. edits are bold.
The annotation of the Assert is causing a problem. You must set that the null value is valid in the Assert.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.