How to combine Translatable and Sluggable from Doc

2019-04-08 14:15发布

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;

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

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!

1条回答
聊天终结者
2楼-- · 2019-04-08 14:53

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!

The code then looks something like:

...
->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();

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).

查看更多
登录 后发表回答