paysera / lib-gedmo-translatable-integration-bundle
本包的最新版本(3.1.1)没有提供许可证信息。
此库提供了将 Gedmo Translatable doctrine 扩展轻松集成到您项目中的方法。
3.1.1
2020-11-04 14:47 UTC
Requires
- php: ^7.0
- doctrine/orm: ^2.0
- gedmo/doctrine-extensions: ^2.0
- stof/doctrine-extensions-bundle: ^1.2.2
- symfony/config: ^3.0 || ^4.0
- symfony/dependency-injection: ^3.0 || ^4.0
- symfony/http-kernel: ^3.0 || ^4.0
- symfony/routing: ^3.0 || ^4.0
- symfony/security-core: ^3.0 || ^4.0
- symfony/validator: ^3.0 || ^4.0
Requires (Dev)
- paysera/lib-php-cs-fixer-config: ^2.3
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2024-09-04 23:14:20 UTC
README
此库提供了将 Gedmo Translatable doctrine 扩展轻松集成到您项目中的方法。
为什么?
有时管理实体翻译可能会变得繁重,这给开发者增加了不必要的认知负担。此库通过自动加载所有翻译并自动翻译实体来帮助您,而无需手动操作。
安装
将 new Paysera\Bundle\GedmoTranslatableIntegrationBundle\PayseraGedmoTranslatableIntegrationBundle()
添加到您的项目 Symfony 内核包中。
添加 config.yml
paysera_gedmo_translatable_integration: default_locale: '<default_locale>'
default_locale
- 这是您的应用程序默认区域设置。
使用方法
定义可翻译实体
<?php namespace App\Entity; use Paysera\Bundle\GedmoTranslatableIntegrationBundle\Entity\TranslatableEntityInterface; use Paysera\Bundle\GedmoTranslatableIntegrationBundle\Entity\TranslatableEntityTrait; class Entity implements TranslatableEntityInterface { use TranslatableEntityTrait; // ... properties // ... setters/getters }
为 Gedmo Translatable 完成Doctrine ORM配置以进行可翻译属性,请按照官方 文档 进行。
翻译实体或读取翻译
使用包
<?php namespace App\Service; use App\Entity\Post; use Paysera\Bundle\GedmoTranslatableIntegrationBundle\Entity\Translation; class PostManager { public function translate(Post $post) { $post->addTranslations('name', [ 'lt' => 'Translation for LT', 'en' => 'Translation for EN', ]); // Done. Bundle will seamlessly make sure, that this entity will be translated using Gedmo extension. } /** * @param Post $post * @return Translation[] */ public function getTranslations(Post $post): array { return $post->getTranslations('name'); // Done. Bundle makes sure that translations are lazy-loaded, when accessed. This is done without necessity to // manually fetch from repositories. } }
不使用包
<?php namespace App\Service; use App\Entity\Post; class PostManager { private $translationRepository; public function translate(Post $post, array $translations) { // Your translation structure might be different. Now Post object is coupled with translations and have to be // passed and moved along. foreach ($translations as $field => $translation) { foreach ($translation as $locale => $value) { $this->translationRepository->translate($post, $field, $locale, $value); } } } public function getTranslations(Post $post) { // Not lazy-loaded. Now you have to fetch all translations and set them somewhere, but not sure if they will be // accessed. // Or implement custom repository method by extending the default Gedmo repository. $result = []; $translations = $this->translationRepository->findTranslations($post); foreach ($translations as $locale => $translation) { foreach ($translation as $property => $value) { if ($property === 'name') { $result[$locale] = $value; } } } return $result; } }
修改查询以连接翻译表
在您可能想要连接翻译表以选择翻译的情况下,您可以使用 \Paysera\Bundle\GedmoTranslatableIntegrationBundle\Service\QueryBuilderTranslationSearchModifier
。
<?php namespace App\Service; use App\Entity\Post; use App\Entity\PostFilter; use App\Repository\PostRepository; use Paysera\Bundle\GedmoTranslatableIntegrationBundle\Entity\TranslationSearchConfiguration; use Paysera\Bundle\GedmoTranslatableIntegrationBundle\Service\QueryBuilderTranslationSearchModifier; class PostManager { /** * @var QueryBuilderTranslationSearchModifier */ private $queryBuilderTranslationSearchModifier; /** * @var PostRepository */ private $postRepository; public function findPostsByFilter(PostFilter $filter) { // Let's imagine here you execute your select, andWhere methods to construct a query builder that would search // for posts that match the filter. $queryBuilder = $this->postRepository->createQueryBuilderFromFilter($filter); // Modify the query builder to join the translations table and also select posts that match the translation. $this->queryBuilderTranslationSearchModifier->modifyQueryBuilder( $queryBuilder, (new TranslationSearchConfiguration()) ->setAlias('p') ->setClassName(Post::class) ->setField('name') ->setLocale('lt') ->setValue(sprintf('%%%s%%', addcslashes($filter->getName(), '%_'))) ); return $queryBuilder->getQuery()->getResult(); } }