使用在Symfony 4中可翻译的DoctrineBehaviors?

时间:2018-11-21 17:31:32

标签: symfony symfony4

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

可翻译实体:

<?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 表中没有任何内容。

Bundles.php:

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

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

1 个答案:

答案 0 :(得分:0)

我不知道您是否找到了答案(希望如此),但是可以,您可以在Symfony 4中使用KnpLabs / DoctrineBehaviors。也许,您只需要稍等片刻即可进行更新。