Download the PHP package secit-pl/entity-translation-bundle without Composer
On this page you can find all versions of the php package secit-pl/entity-translation-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download secit-pl/entity-translation-bundle
More information about secit-pl/entity-translation-bundle
Files in secit-pl/entity-translation-bundle
Package entity-translation-bundle
Short Description .
License MIT
Homepage http://secit.pl
Informations about the package entity-translation-bundle
Entity Translation Bundle
Doctrine entity translations Symfony 4.x.
Installation
From the command line run
Usage
Configuration
Open configuration file ./config/packages/entity_translation.yaml
. If file not exists create it.
The file content shoud looks somehting like this:
Entity
Key rules
- Translatable entity should implements
SecIT\EntityTranslationBundle\Translations\TranslatableInterface
- Translation entity should be called
Translation
and be placed in namespace same as translatable class name. For example, if translatable entity isApp\Entity\Shop
the translation class should beApp\Entity\Shop\Translation
- Translation should extends
SecIT\EntityTranslationBundle\Entity\AbstractTranslation
Example
Let's say we have following entity and we want to translate the name
and description
fields.
Other field should not be translated.
./src/Entity/Shop.php
We need to split the file to two separeted files. One will contain the common part of each translation and one will contain the fields we want to translate.
First we need to create a Shop Translation entity. The entity should be placed in namespace App\Entity\Shop
.
./src/Entity/Shop/Translation.php
What has been done:
- We created a class
Translation
in namespaceApp\Entity\Shop
- The class extends
SecIT\EntityTranslationBundle\Entity\AbstractTranslation
- We created a standard entity with
name
anddescription
fields copied from original entity - To improve type hinting we added
@method Shop getTranslatable()
Next it's time to change the base entity.
./src/Entity/Shop.php
What has been done:
- We added class implements
SecIT\EntityTranslationBundle\Translations\TranslatableInterface
- We used default implementation of implemented interface using
SecIT\EntityTranslationBundle\Translations\TranslatableTrait
- We added constructor to initialize translations collection
- We removed
name
anddescription
setters getName
andgetDescription
was changed to have easier way to get translated values and to be more backward compatible- Type hinting was added
Now we need to update the database schema by php bin/console doctrine:schema:update --force
and that's it. Now our entity is translatable.
Remember, if you had a data in database you should move it manually to the new database schema!
Usage
Using translatable entity in forms
You need to create two classes. One for Translatable entity, and one for translation.
./src/Form/ShopType.php
./src/Form/Shop/TranslationType.php
Now you can use App\Form\ShopType
like a normal Symfony form.
Translations will be handled automatically.
All versions of entity-translation-bundle with dependencies
symfony/config Version ^4.4 || ^5.0 || ^6.0
symfony/dependency-injection Version ^4.4 || ^5.0 || ^6.0
symfony/form Version ^4.4 || ^5.0 || ^6.0
symfony/framework-bundle Version ^4.4 || ^5.0 || ^6.0
symfony/http-kernel Version ^4.4 || ^5.0 || ^6.0
symfony/intl Version ^4.4 || ^5.0 || ^6.0
symfony/options-resolver Version ^4.4 || ^5.0 || ^6.0
symfony/validator Version ^4.4 || ^5.0 || ^6.0
doctrine/doctrine-bundle Version ^2.3
doctrine/orm Version ^2.9