【发布时间】:2023-04-02 08:38:01
【问题描述】:
我正在使用 symfony 3 并尝试使用 mysql db 翻译类别。这就是我使用 KnpLabs/DoctrineBehaviors 的原因,这对 symfony 来说应该是最好的。
我已经按照文档中的描述做了所有事情。
类别:
/**
* MdCategories
*
* @ORM\Table(name="md_category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category implements ORMBehaviors\Tree\NodeInterface, \ArrayAccess
{
use ORMBehaviors\Translatable\Translatable,
ORMBehaviors\Sortable\Sortable,
ORMBehaviors\Tree\Node;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="NONE")
*/
protected $id;
类别翻译:
/** * @ORM\Table(name="md_category_translation") * @ORM\实体 */ 类 CategoryTranslation { 使用 ORMBehaviors\Translatable\Translation;
/**
* @var name
* @ORM\Column(type="string", length=120, nullable=false)
*/
protected $name;
/**
* @var route
*
* @ORM\Column(type="string", length=150)
*/
protected $route;
/**
* @var metaKey
*
* @ORM\Column(type="string", length=255)
*/
protected $metaKey;
/**
* @var metaTitle
*
* @ORM\Column(type="string", length=100)
*/
protected $metaTitle;
/**
* @var metaDescription
*
* @ORM\Column(type="string", length=120)
*/
protected $metaDescription;
/**
* @return name
*/
public function getName()
{
return $this->name;
}
/**
* @param name $name
* @return Categories
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return route
*/
public function getRoute()
{
return $this->route;
}
/**
* @param route $route
* @return Categories
*/
public function setRoute($route)
{
$this->route = $route;
return $this;
}
/**
* @return metaKey
*/
public function getMetaKey()
{
return $this->metaKey;
}
/**
* @param metaKey $metaKey
* @return Categories
*/
public function setMetaKey($metaKey)
{
$this->metaKey = $metaKey;
return $this;
}
/**
* @return metaTitle
*/
public function getMetaTitle()
{
return $this->metaTitle;
}
/**
* @param metaTitle $metaTitle
* @return Categories
*/
public function setMetaTitle($metaTitle)
{
$this->metaTitle = $metaTitle;
return $this;
}
/**
* @return metaDescription
*/
public function getMetaDescription()
{
return $this->metaDescription;
}
public function setMetaDescription($metaDescription)
{
$this->metaDescription = $metaDescription;
return $this;
}
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
}
CategoryRepository:
class CategoryRepository extends EntityRepository
{
use ORMBehaviors\Tree\Tree;
}
config.yml:
knp_doctrine_behaviors:
translatable: true
tree: true
sortable: true
# All others behaviors are disabled
并注册捆绑包
new Knp\DoctrineBehaviors\Bundle\DoctrineBehaviorsBundle(),
数据库生成正确
但是如何使用正确的本地填充 categoryTranslation 数据?
$category = new Category;
$category->setCurrentLocale('de');
$category->setId(1); // tree nodes need an id to construct path.
$category->setName('Foobar');
$em->persist($category);
$em->flush();
这不起作用!
我很惊讶,在 symfony 的订阅事件中没有 KNP Translationsubscriber 被监听
【问题讨论】:
标签: symfony translation