2

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.orm.yml file

oneToOne:
  userItem:
    targetEntity: SOA\AXBundle\Entity\Items
    cascade: ["remove", "persist"]
    joinColumn:
      name: id_item        
      referencedColumnName: id 
      nullable: true

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.

4

0 に答える 0