This has to be something really simple, but i just can't figure it out.
I got a Parent entity with a Child one-to-one unidirectional relation, the Parent entity being the owning side:
/**
* @ORM\OneToOne(targetEntity="Child", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\JoinColumn(name="child_id", referencedColumnName="id")}
*/
protected $Child;
The form has the ChildType added without it being required, but the ChildType has some required fields:
$builder->add('child', new ChildType(), [
'required' => false,
]);
In a create action there are no problems:
With all the Child fields being empty, the association stays null, the validation constraints for the Child relation are ignored, and the Child relation doesnt get created even though it has some required fields.
When some of the Child fields are filled in, the association is created and the Child entity gets persisted if validation passes.
So far working as expected..
When updating the data set, everything goes well exactly the same way as when creating. Perfect
Now a situation where i want the child entity removed, this should be as simple as clearing all child fields, since the child is not required. Emtying all fields however, still triggers validation and results in doctrine updating the relation with all NULL fields.
What i tried:
Created a preUpdate listener for Doctrine to delete the relation if all fields are null. The problem is that you can't get this far without disabling validation. So this can't be the right way. One way would be to just use a callback constraints that returns true if all fields are null, but this would render all the property constraints useless for all one-to-one cases. Cant be true..
So far no clue what now, nothing in the documentation either, only mass use-cases of x-to-many...
Is it possible to somehow use a different validation group or no validation PostSubmit inside the symfony form or some other way to not use any validation if all fields are NULL of the child entity?