70216

1 to 1..0 relationship in an embedded form with doctrine

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 <strong>create</strong> an user, the item can be null? <strong>(and not to insert a new row and just leave id_item null)</strong>

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 <strong>add</strong> an user where it has no item (<strong>user item fields are blank</strong>), I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

<strong>It is trying to insert the item, with null values.</strong>

While I can live with an 1 to 1 relationship, I would like to learn how to make an 1 to 1..0 relationship.

<strong>edited with the real problem.</strong> edits are bold.

Answer1:

The annotation of the Assert is causing a problem. You must set that the null value is valid in the Assert.

Recommend

  • Missing value for primary key id Doctrine Symfony2
  • Entity Not Found error in symfony2 Form
  • onetomany unidirectional with jointable setup using jpa
  • How to not return an specific column using PagingAndSortingRepository
  • Using @OneToOne in PlayFramework 2 / Ebean ORM where the child and parent shares the same primary ke
  • Can a single model object be a parent of multiple child objects?
  • Symfony2 Change checkbox values from 0/1 to 'no'/'yes'
  • Multiple @ManyToMany sets from one join table
  • PrePersit current user
  • How to avoid child of child objects in Spring JPA?
  • many to many association. Datas doesn't delete
  • Hibernate - encrypted reference by relations on usernames
  • Symfony2 - ReferencedColumnName id is null
  • Hibernate Criteria with self join
  • Doctrine join bypass lazy loading
  • Can't insert value to mapped column in Doctrine 2 in OneToOne relation
  • How to write Hibernate HQL query which remove all “grand children” elements?
  • declare a transaction in ejb 3.0
  • FullTextHibernateSessionProxy is not visible from class loader
  • Cascade persist error with JPA and EclipseLink on ManyToOne relationship
  • hibernate cascade - update child to null
  • How to return objects from multiple entity by joining by using spring data jpa?
  • Django/Python: How to iterate through a list in a dictionary for migration/migrate data
  • How to remove relationship between many to many table with JPA and Hibernate
  • could not insert select SCOPE_IDENTITY() fluent nhibernate one to many
  • applying background to first, and every third row in a dynamically generated table
  • How to set the id of a foreign key id #sf2 #doctrine2
  • Hibernate one-to-many mapping eager fetch not working
  • symfony render json_array entity type and save using form
  • Can I use jquery to blank textarea fields or ajax like input boxes?
  • JPA CascadeType.REMOVE not working
  • Does Context/Scoping of a SQLAlchemy Session Require Non-Automatic Object/Attribute Expiration?
  • Doctrine2 inverse persistance not working in nested forms
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • Cypher - matching two different possible paths and return both
  • ListItem.Attributes.Add not working
  • php design question - will a Helper help here?
  • how does django model after text[] in postgresql [duplicate]
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?