Skip to content

Commit

Permalink
Merge branch 'hotfix/4.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
qzminski committed Jan 20, 2020
2 parents 8fef15a + a115793 commit f43270e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/Model/Multilingual.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,26 @@ public function getAlias($language, $aliasColumnName = 'alias')
{
// Do not load any translation if already target language
$langColumn = static::getLangColumn();
$fallbackLang = static::getFallbackLanguage();

if ($language === $fallbackLang && !$this->{$langColumn}) {
return $this->{$aliasColumnName};
}

if ($language === $this->{$langColumn}) {
return $this->{$aliasColumnName};
}

// Try to load the translated model
$translatedModel = static::findByPk($this->id, ['language' => $language]);
$translatedModel = static::findByPk($this->getLanguageId(), ['language' => $language]);

if (null === $translatedModel) {
// Get fallback
$fallbackLang = static::getFallbackLanguage();

if ($language === $fallbackLang) {
return $this->{$aliasColumnName};
}

$fallbackModel = static::findByPk($this->id, ['language' => $fallbackLang]);
$fallbackModel = static::findByPk($this->getLanguageId(), ['language' => $fallbackLang]);

return $fallbackModel->{$aliasColumnName};
}
Expand All @@ -84,7 +87,7 @@ public static function findByAlias($alias, $aliasColumnName = 'alias', $options
$table = static::getTable();
$options = array_merge([
'limit' => 1,
'column' => ["($table.$aliasColumnName=?"],
'column' => ["$table.$aliasColumnName=?"],
'value' => [$alias],
'return' => 'Model',
],
Expand Down
9 changes: 8 additions & 1 deletion src/QueryBuilder/MultilingualQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Terminal42\DcMultilingualBundle\QueryBuilder;

use Doctrine\DBAL\Query\QueryBuilder;
use function array_intersect;

class MultilingualQueryBuilder implements MultilingualQueryBuilderInterface
{
Expand Down Expand Up @@ -107,8 +108,14 @@ public function buildQueryBuilderForFind($language)
{
$this->qb->resetQueryParts();

// Always translate system columns
$systemColumns = ['id', $this->langColumnName, $this->pidColumnName];
foreach ($systemColumns as $field) {
$this->qb->addSelect("IFNULL(translation.$field, {$this->table}.$field) AS $field");
}

// Regular fields
foreach (array_diff($this->regularFields, $this->translatableFields) as $field) {
foreach (array_diff($this->regularFields, $this->translatableFields, $systemColumns) as $field) {
$this->qb->addSelect("{$this->table}.$field");
}

Expand Down

0 comments on commit f43270e

Please sign in to comment.