KnpLabs / DoctrineBehaviors

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

translate not working #605

Closed langziyang closed 3 years ago

langziyang commented 3 years ago

sorry,i am not see the $category->mergeNewTranslations();

i follow the doc,create Category and CategoryTranslation entity

Category:

<?php

namespace App\Entity;

use App\Repository\CategoryRepository;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;

/**
 * @ORM\Entity(repositoryClass=CategoryRepository::class)
 */
class Category implements TranslatableInterface
{
    use TranslatableTrait;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    public function getId(): ?int
    {
        return $this->id;
    }
}

CategoryTranslation:


namespace App\Entity;

use App\Repository\CategoryTranslationRepository;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;

/**
 * @ORM\Entity(repositoryClass=CategoryTranslationRepository::class)
 */
class CategoryTranslation implements TranslationInterface
{
    use TranslationTrait;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
}

and on my CategoryFixtures:

<?php

namespace App\DataFixtures;

use App\Entity\Category;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class CategoryFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $category = new Category();
        $category->translate('en')->setName('Home');
        $category->translate('zh_cn')->setName('首页');
        $manager->persist($category);
        $manager->flush();
    }
}

when i load the fixture,only category table has on data.category_translation table has nothing

category table: id int(11) AUTO_INCREMENT

category_translation : id int(11) AUTO_INCREMENT translatable_id int(11) name varchar(255) locale varchar(5)

TomasVotruba commented 3 years ago

Hi, how dis you solve it? Just so others know what to do in the same situation.

langziyang commented 3 years ago

Hi, how dis you solve it? Just so others know what to do in the same situation.

need $category->mergeNewTranslations();

TomasVotruba commented 3 years ago

Thanks