Skip to content

Support for unique fields (e.g. slug, uuid) when translating entities #1

@CreativeNative

Description

@CreativeNative

Problem

When translating entities that contain unique fields (e.g. uuid, slug), the current translation process copies these values 1:1 to the new translation.
This leads to unique constraint violations on the database level:

  • uuid → copied, violates UNIQUE
  • slug → copied, violates UNIQUE

Attributes like #[EmptyOnTranslate] or #[SharedAmongstTranslations] do not solve this problem for unique fields.


Example

#[ORM\Column(type: "string", length: 255, unique: true)]
private string $slug;

When creating a translation, the slug value is copied → UNIQUE constraint failed.


Possible Solutions

  1. Schema-based fix (quick solution):
    Use a composite unique index on slug + locale instead of a global unique on slug.
    See Quick Fix for unique fields in the README.

    #[ORM\UniqueConstraint(
        name: "uniq_slug_locale",
        columns: ["slug", "locale"]
    )]
  2. Bundle improvement (long-term):
    Add a handler for unique fields in the translator:

    • Respect #[EmptyOnTranslate] → set field to NULL
    • Regenerate new values for uuid or slug (e.g. via SluggerInterface, Uuid::v4())
    • Throw a clear exception if no strategy is possible

Why it matters

Without handling unique fields, entities with slugs or UUIDs cannot be translated safely.
Supporting this would make the bundle more robust for real-world use cases.

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