Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine Translatable and Sluggable from DoctrineExtensions?

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!

like image 983
stefax Avatar asked Oct 21 '22 18:10

stefax


1 Answers

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

like image 63
stefax Avatar answered Oct 27 '22 14:10

stefax