KnpLabs / DoctrineBehaviors

Doctrine2 behavior traits that help handling Blameable, Loggable, Sluggable, SoftDeletable, Uuidable, Timestampable, Translatable, Tree behavior
http://knplabs.com
MIT License
914 stars 294 forks source link

Add translation to existing entity #576

Closed bettinz closed 4 years ago

bettinz commented 4 years ago

Hello, I'm doing something like that: Entity\Category

private $id;

Entity\CategoryTranslation

private $name;
$category = new Category;

$category->translate('en')->setName('nameEn');
$category->translate('es')->setName('nameEs');
$category->mergeNewTranslations();

$this->entityManager->persist($category);

$category->mergeNewTranslations();
$this->entityManager->flush();

$alreadyCreatedCategory = $this->entityManager->getRepository('App\Entity\CategoryTranslation')->findOneBy([
            'name' => 'nameEn',
            'locale' => 'en',
        ]);
$category2 = $alreadyCreatedCategory->getTranslatable();

$category2->translate('it')->setName('nameIt');

$this->entityManager->persist($category2);

$category2->mergeNewTranslations();
$this->entityManager->flush();

I was hoping that the new translation will be added to the category but it doesn't work. Instead, it replace the name without updating the locale. Am I doing something wrong? Thanks

tpatartmajeur commented 4 years ago

Hi, I had the same problem and I added this $category2->translate('it', false)->setName('nameIt');

By doing this the translate method does not use the fallback.

bettinz commented 4 years ago

THANK YOU @tpatartmajeur 🙏