我正在使用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;
CategoryTranslation:
/ ** * @ORM \ Table(name =“md_category_translation”) * @ORM \ Entity * / class 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(),
正确生成数据库
但是如何使用正确的本地填充“翻译数据”类别?
$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();
这不起作用!
我很惊讶,没有KNP Translationsubscriber在symfony的描述事件中被听取
答案 0 :(得分:0)
这是解决方案。
$category = new Category;
$category->translate('de')->setName('Foobar');
$em->persist($category);
$category->mergeNewTranslations();
$em->flush();