Skip to content

Commit

Permalink
Merge pull request #61 from dmolineus/fix/system-columns
Browse files Browse the repository at this point in the history
Id, language pid and language column not translated
  • Loading branch information
qzminski authored Jan 20, 2020
2 parents 4891d7e + bbee4ad commit a115793
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Model/Multilingual.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public function getAlias($language, $aliasColumnName = 'alias')
}

// 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
if ($language === $fallbackLang) {
return $this->{$aliasColumnName};
}

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

return $fallbackModel->{$aliasColumnName};
}
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 a115793

Please sign in to comment.