Prezent / doctrine-translatable

Translatable behaviour extension for Doctrine2
MIT License
26 stars 17 forks source link

Translations collection not indexed by locale #20

Closed mcordoba closed 9 years ago

mcordoba commented 9 years ago

Hi,

I am using your extension for symfony in order to get translation working but Im having a problem.

I am following the instructions in the documentation and it says you have to include this method in your entity:

    public function translate($locale = null)
    {
        if (null === $locale) {
            $locale = $this->currentLocale;
        }

        if (!$locale) {
            throw new \RuntimeException('No locale has been set and currentLocale is empty');
        }

        if ($this->currentTranslation && $this->currentTranslation->getLocale() === $locale) {
            return $this->currentTranslation;
        }

        if (!$translation = $this->translations->get($locale)) {
            $translation = new PageTranslation();
            $translation->setLocale($locale);
            $this->addTranslation($translation);
        }

        $this->currentTranslation = $translation;
        return $translation;
    }

I think the translation collection is not being indexed by the locale string because in the line with:

$this->translations->get($locale) is always returning null.

I have been reviewing your code and I cant find where is the collection being indexed by the locale string.

I'll appreciate any help.

Thank you in advance.

sandermarechal commented 9 years ago

There is an indexBy on the OneToMany relation from the Translatable object to the Translation. If you map this relation yourself, you should add the indexBy yourself. If you let this extension handle the relation then the indexBy is defined here: https://github.com/Prezent/doctrine-translatable/blob/master/lib/Prezent/Doctrine/Translatable/EventListener/TranslatableListener.php#L172

LouTerrailloune commented 8 years ago

In having the same issue. The problem exists when the entity is just created (not loaded from database) ans we use the AbstractTranslatable. AbstractTranslatable does not index the array of translation by the locale, so if you try to use translate() before saving the entity a new empty translation is created.

sandermarechal commented 8 years ago

@LouTerrailloune Should be fixed in master. Can you test it? If it works I'll tag a new version.

LouTerrailloune commented 8 years ago

@sandermarechal I didn't test it but it's seems to be ok.

sandermarechal commented 7 years ago

Tagged as 1.1.2. Thanks!