This PR adds a translatable DBAL type that allows type-safe declarations of entity fields holding translatable values, even without using union types. The requirement for that is that the underlying values that are are strings and would use either the string or text column types in Doctrine ORM.
Example entity from the README:
<?php
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Annotation as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
/**
* @ORM\Entity
* @Polyglot\Locale(primary="en_GB")
*/
class Document
{
// ... fields and collections omitted for brevity
/**
* @ORM\Column(type="translatable")
* @Polyglot\Translatable
*/
private TranslatableInterface $text;
// ...
}
The translatable column type behaves like the string column type, but allows you to type hint for TranslatableInterface only (no TranslatableInterface|string union required). If you want the database column to behave like a text type, you can use @ORM\Column(type="translatable", options={"use_text_column": true}).
Note that it is not necessary to do this in the translations class (DocumentTranslation in the README examples), since that class represents the values of a single locale only and never contains Translatable instances.
This PR adds a
translatable
DBAL type that allows type-safe declarations of entity fields holding translatable values, even without using union types. The requirement for that is that the underlying values that are arestring
s and would use either thestring
ortext
column types in Doctrine ORM.Example entity from the README:
The
translatable
column type behaves like thestring
column type, but allows you to type hint forTranslatableInterface
only (noTranslatableInterface|string
union required). If you want the database column to behave like atext
type, you can use@ORM\Column(type="translatable", options={"use_text_column": true})
.Note that it is not necessary to do this in the translations class (
DocumentTranslation
in the README examples), since that class represents the values of a single locale only and never containsTranslatable
instances.