Created
July 16, 2011 08:36
-
-
Save l3pp4rd/1086164 to your computer and use it in GitHub Desktop.
Translatable default locale query hint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$qb = $em->createQueryBuilder(); | |
$qb->select('a') | |
->from('Entity\\Article', 'a') | |
->add('where', $qb->expr()->not($qb->expr()->eq('a.title', ':title'))) | |
->setParameter('title', 'NA') | |
; | |
$translationListener->setTranslatableLocale('es'); | |
$translationListener->setDefaultLocale('en'); | |
$translationListener->setTranslationFallback(true); | |
$q = $qb->getQuery(); | |
$q->setHint( | |
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, | |
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker' | |
); | |
$result = $q->getArrayResult(); | |
// would produce sql like: | |
/* | |
SELECT a0_.id AS id0, | |
COALESCE(t1_.content, t2_.content) AS title1, | |
COALESCE(t3_.content, t4_.content) AS content2, | |
a0_.title AS title_fallback3, | |
a0_.content AS content_fallback4 | |
FROM Article a0_ | |
LEFT JOIN ext_translations t1_ ON t1_.locale = 'es' AND t1_.object_class = 'Entity\\Article' | |
AND t1_.field = 'title' AND t1_.foreign_key = a0_.id | |
LEFT JOIN ext_translations t2_ ON t2_.locale = 'en' AND t2_.object_class = 'Entity\\Article' | |
AND t2_.field = 'title' AND t2_.foreign_key = a0_.id | |
LEFT JOIN ext_translations t3_ ON t3_.locale = 'es' AND t3_.object_class = 'Entity\\Article' | |
AND t3_.field = 'content' AND t3_.foreign_key = a0_.id | |
LEFT JOIN ext_translations t4_ ON t4_.locale = 'en' AND t4_.object_class = 'Entity\\Article' | |
AND t4_.field = 'content' AND t4_.foreign_key = a0_.id | |
WHERE NOT (COALESCE(t1_.content, t2_.content) = 'NA'); | |
*/ | |
// Which would join translations of current locale "en" and default locale "es" | |
// in case if there is no translation in "en" for a specific field, it will use "es" translation | |
// and in case if their both translations are empty it will use original record value as a fallback on hydration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment