如何从DoctrineExtensions组合可翻译和可压缩? [英] How to combine Translatable and Sluggable from DoctrineExtensions?

查看:143
本文介绍了如何从DoctrineExtensions组合可翻译和可压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 https://github.com/stof/StofDoctrineExtensionsBundle ,并使用可翻译和可压缩在国家实体的特定字段中:

I have installed https://github.com/stof/StofDoctrineExtensionsBundle and use both Translatable and Sluggable on a specific field in a Country entity:

...
class Country
{
    ...
    /**
     * @Gedmo\Translatable
     * @Gedmo\Slug(fields={"name"})
     * @ORM\Column(length=255, nullable=false)
     */
    private $slug;

页面的URL应为... / country / france为英语用户和... / land / frankreich为德国用户。

The URL of a page should be .../country/france for English users and .../land/frankreich for German users.

在控制器中,我得到特定语言的s language声,并通过本地区特定的s and过滤,我想要检索一个国家实体。

In a controller I get the slug in the specific language and filtered by this locale-specific slug I want to retrieve a country entity.

我没有在这里或文档中找到有关如何做的事情。

I haven't found anything here or in the docs about how to do that.

感谢任何关于如何解决的提示!

Thanks for any hint on how to solve that!

推荐答案

刚刚在这篇博客文章中找到了解决方案。该解决方案正在使用TranslationWalker的ORM查询提示来自动加入转换表,因此您可以通过任何翻译字段进行排序或过滤。这很棒!

Just found the solution in this blog article. The solution is using the ORM query hint of the TranslationWalker to automatically join the translation table, so you can order by or filter by any translated field. This is great!

代码看起来像:

...
->createQuery('SELECT...FROM MyFooBundle:Country c WHERE c.slug = :slug...)
->setParameter('slug', $slug)
->setHint(
    \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
    'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
)
->getSingleResult();

顺便说一句:如果你想使用回退(即如果没有具体的翻译可用,默认字符串/文本),然后通过setTranslationFallback方法调用(在doctrine_extensions.yml中)为gedmo.listener.translatable服务配置它。

By the way: If you want to use fallback (i.e. if no specific translation is available, take the default string/text), then just configure it for your gedmo.listener.translatable service through the setTranslationFallback method call (in doctrine_extensions.yml).

这篇关于如何从DoctrineExtensions组合可翻译和可压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆