Skip to content

Latest commit

 

History

History
executable file
·
73 lines (61 loc) · 2.03 KB

yaml-mapping.md

File metadata and controls

executable file
·
73 lines (61 loc) · 2.03 KB

Yaml mapping

If you don't (want to) use annotations for your doctrine mappings, you can use the yaml driver. If you use the DoctrineAdapter to create your driver, the Yaml driver will be used automatically.

Manually adding the Yaml driver

You can enable the Yaml drive manually using the following code. This allows you to keep the Doctrine mappings separate from your translatable mappings.

use Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator;
use Prezent\Doctrine\Translatable\Mapping\Driver\YamlDriver;

$locator = new DefaultFileLocator(...);
$yamlDriver = new YamlDriver($locator);
$metadataFactory  = new MetadataFactory($yamlDriver);

Yaml mapping example

# BlogPost.orm.yml
BlogPost:
    prezent:
        translatable:
            field: translations # optional (default: translations)
            targetEntity: BlogPostTranslation # optional (default: [EntityName]Translation)
            currentLocale: currentLocale # optional
            fallbackLocale: fallbackLocale # optional
    fields:
        ...
# BlogPostTranslation.orm.yml
BlogPostTranslation:
    prezent:
        translatable:
            field: translatable # optional (default: translatable)
            targetEntity: BlogPost # optional (default: entity name without "Translation" suffix)
            locale: locale # optional (default: locale)

and if you wants to manually define the relations (this is not required, they are generated by prezent/translatable) :

# BlogPost.orm.yml
BlogPost:
    ...
    oneToMany:
        translations:
            targetEntity: BlogPostTranslation
            mappedBy: translatable
            cascade: ["persist", "remove"]
            indexBy: locale
# BlogPostTranslation.orm.yml
BlogPostTranslation:
    ...
    manyToOne:
        translatable:
            targetEntity: BlogPost
            inversedBy: translations
            joinColumn:
                name: translatable_id
                referencedColumnName: id
                onDelete: "CASCADE"