GiD*_*iDo 5 php doctrine translate symfony doctrine-orm
我遇到了以下问题:我想获取特定语言环境的学说实体,而不破坏我的 Symfony 应用程序的默认行为。
这是我的实体之一的示例:
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass="ProductRepository")
* @ORM\Table(name="product")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
*/
class Product
{
/**
* @var integer $id
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="name", type="string")
* @Gedmo\Translatable
*/
protected $name;
// ...
}
Run Code Online (Sandbox Code Playgroud)
相关学说库的一部分:
class ProductRepository extends \Doctrine\ORM\EntityRepository
{
public function findOneProductInLocale($id, $locale)
{
$qb = $this->createQueryBuilder('p')
->select('p')
->where('p.id = :id')
->setMaxResults(1)
->setParameter('id', $id);
;
$query = $qb->getQuery();
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
// force Gedmo Translatable to not use current locale
$query->setHint(
\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
$locale
);
$query->setHint(
\Gedmo\Translatable\TranslatableListener::HINT_FALLBACK,
1
);
return $query->getOneOrNullResult();
}
}
Run Code Online (Sandbox Code Playgroud)
和我的脚本的一部分:
// default Locale: en
// request Locale: de
$repo = $em->getRepository('Acme\\Entity\\Product');
$product1 = $repo->findOneById($id);
echo $product1->getName(); // return 'Name (DE)'
$product_de = $repo->findOneProductInLocale($id, 'de');
echo $product_de->getName(); // return 'Name (DE)';
$product_en = $repo->findOneProductInLocale($id, 'en');
echo $product_en->getName(); // return 'Name (EN)'
echo $product1->getName(); // return 'Name (EN)' instead of 'Name (DE)' !! <-- What is wrong?
// even if I refetch a product
$product2 = $repo->findOneById($id);
echo $product2->getName(); // return 'Name (EN)' without taking anymore in account the current locale
Run Code Online (Sandbox Code Playgroud)
现在有人知道为什么这没有按预期工作吗?我的实施有问题吗ProductRepository::findOneProductInLocale()?
欢迎任何帮助或提示。
我知道我的回答有点晚了,但我面临同样的问题并找到了解决方案。我希望它能帮助其他一些开发人员。
你的findOneProductInLocale如果完全没问题。
它按照设计工作- 当您使用时findOneProductInLocale,查询将在给定的语言环境中搜索,但最终的实体将始终在当前语言环境中加载,您无法更改它。
一旦通过当前区域设置找到findOneProductInLocale并加载实体,您就可以使用Gedmo 的方法获取所需的区域设置变体并刷新您的实体,如@umadesign所解释的setTranslatableLocale
// Reload the entity in different languages.
$entity->setTranslatableLocale($locale);
$em->refresh($entity);
Run Code Online (Sandbox Code Playgroud)(可选)您可能需要将setTranslatableLocale方法和伴随属性添加$local到可翻译实体中
class Product {
// ...
/**
* @Gedmo\Locale
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
private $locale;
/**
* Set the locale to use for translation listener
*
* @param string $locale
*
* @return static
*/
public function setTranslatableLocale($locale) {
$this->locale = $locale;
return $this;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)您可以在Gedmo 文档的“基本用法示例”小节中找到完整的说明。
| 归档时间: |
|
| 查看次数: |
5541 次 |
| 最近记录: |