首页 > 解决方案 > 使用可在 Symfony 4 中翻译的 DoctrineBehaviors?

问题描述

我正在尝试在 Symfony 4 中使用 DoctrineBehaviors 可翻译扩展。只需按照文档示例设置测试:

可翻译实体:

<?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;
        }
    }

翻译实体:

    <?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;
        }
    }

测试可翻译实体:

        /**
         * @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', [
            ]);
        }

一个新的 ID 被添加到常见问题表中。但是faqtranslation表中没有保留任何内容。

捆绑包.php:

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

我发现的所有文档似乎都参考 Symfony 3 甚至 Symfony 2,是否可以在 Symfony 4 中使用可翻译的 DoctrineBehaviors ?

标签: symfonysymfony4

解决方案


我不知道你是否找到了答案(我希望你找到了),但是是的,你可以将 KnpLabs/DoctrineBehaviors 与 Symfony 4 一起使用。也许,你只需要等待更长时间才能获得更新。


推荐阅读