Skip to content

Improve #[EmptyOnTranslate] handling for non-nullable and scalar fields #2

@CreativeNative

Description

@CreativeNative

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: title should become an empty string, position should become 0.
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 null if 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions