Rmy5
Rmy5

Reputation: 577

Using DoctrineBehaviors translatable in Symfony 4?

I'm trying to use the DoctrineBehaviors translatable extension in Symfony 4.Just setup a test following the documentation example:

translatable entity:

<?php

     namespace App\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Knp\DoctrineBehaviors\Model as ORMBehaviors;

   /**
     * @ORM\Entity(repositoryClass="App\Repository\FAQRepository")
     */
    class FAQ
    {
        use ORMBehaviors\Translatable\Translatable;

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

        /.**
         * @ORM\Column(type="datetime", nullable=true)
         */
        protected $updatedAt;

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

        public function getUpdatedAt(): ?\DateTimeInterface
        {
            return $this->updatedAt;
        }

        public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
        {
            $this->updatedAt = $updatedAt;

            return $this;
        }
    }

translation entity:

    <?php

    namespace App\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Knp\DoctrineBehaviors\Model as ORMBehaviors;

    /**
     * @ORM\Entity
     */
    class FAQTranslation
    {
        use ORMBehaviors\Translatable\Translation;

        /**
         * @ORM\Column(type="text")
         */
        protected $question;

        /**
         * @ORM\Column(type="text")
         */
        protected $answer;

        /**
         * @ORM\Column(type="integer", nullable=true)
         */
        protected $category;


        public function getQuestion(): ?string
        {
            return $this->question;
        }

        public function setQuestion(string $question): self
        {
            $this->question = $question;

            return $this;
        }

        public function getAnswer(): ?string
        {
            return $this->answer;
        }

        public function setAnswer(string $answer): self
        {
            $this->answer = $answer;

            return $this;
        }


        public function getCategory(): ?int
        {
            return $this->category;
        }

        public function setCategory(?int $category): self
        {
            $this->category = $category;

            return $this;
        }
    }

Testing the translatable entity:

        /**
         * @Route("/test", name="test")
         */
        public function testfaq()
        {
            $em = $this->getDoctrine()->getManager();

            $faq = new FAQ();
            $faq->translate('fr')->setQuestion('Quelle est la couleur ?');
            $faq->translate('en')->setQuestion('What is the color ?');
            $em->persist($faq);

            $faq->mergeNewTranslations();
            $em->flush();

            return $this->render('app/test.html.twig', [
            ]);
        }

A new ID is added in the faq table. But nothing is persisted in the faqtranslation table.

Bundles.php :

    Knp\DoctrineBehaviors\Bundle\DoctrineBehaviorsBundle::class => ['all' => true],

All the documentations I found seem to refer to Symfony 3 or even Symfony 2, is it possible to use DoctrineBehaviors translatable in Symfony 4 ?

Upvotes: 0

Views: 885

Answers (1)

No_or_yes
No_or_yes

Reputation: 715

I don't know if you found your answer since (I hope you did), but yes you can use KnpLabs/DoctrineBehaviors with Symfony 4. Maybe, you just needed to wait a little longer for an update.

Upvotes: 1

Related Questions