-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Problem
The current implementation of #[EmptyOnTranslate] only works reliably if:
- the field is nullable, or
- the field is a
Doctrine\Common\Collections\Collection.
In these cases the handlers set the value to null or to a new ArrayCollection.
For other field types (e.g. non-nullable string or int), nothing happens, and the field keeps its original value from the source entity.
This leads to unexpected behavior and can break database constraints.
Example
#[ORM\Column(type: "string", length: 255, nullable: false)]
#[EmptyOnTranslate]
private string $title;
#[ORM\Column(type: "integer", nullable: false)]
#[EmptyOnTranslate]
private int $position;Expected:
titleshould become an empty string,positionshould become0.
Actual: values are copied from the source entity.
Suggested Improvement
#[EmptyOnTranslate] should respect the field type and nullability:
- Nullable field → set to
null - Non-nullable string → set to
"" - Non-nullable integer/float → set to
0/0.0 - Collection → set to
new ArrayCollection() - Other objects / embeddables → configurable or
nullif nullable, otherwise throw a clear exception
This ensures that the translated entity always gets a consistent, type-safe default value.
Why it matters
Currently #[EmptyOnTranslate] throws only a LogicException if the property is not nullable in the EntityTranslator.
Improving its behavior would make translations more predictable and prevent unexpected unique constraint violations or database errors.