symfony - 使用可在 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 ?
解决方案
我不知道你是否找到了答案(我希望你找到了),但是是的,你可以将 KnpLabs/DoctrineBehaviors 与 Symfony 4 一起使用。也许,你只需要等待更长时间才能获得更新。
推荐阅读
- python - 如何在 Python 中组合列表和字典?
- windows - docker volume create 中的此平台错误不支持如何解决选项
- python - Discord PY 没有获取用户头像
- c++ - 无法绑定到 WSL 2 中 docker 暴露(使用?)的端口
- java - 观察 MutableLiveData 的正确方法是什么
> ? 安卓 - python-3.x - 引发 ValueError 时可以显示索引号吗?
- python - 在日期列表上映射函数时遇到问题
- laravel-8 - Laravel 8:此集合实例上不存在属性 [名称]
- terraform - 从 aws 中删除任何 terraform 资源
- node.js - lambda 在第一次运行时不更新 DynamoDB 数据,但在第二次运行时更新