我使用" Knp Doctrine2 Behaviors"实体翻译和" A2LiX翻译表"以Sonata Admin形式翻译实体。一切都很好。但我无法意识到我应该放入什么" configureDatagridFilters"和" configureListFields" Admin类的方法?
当然,例如,$ datagridMapper-> add(' title');不起作用,因为它是我的" Post"实体,它位于PostTranslation类中,但Admin类适用于Post实体。 Post实体中的__call方法没有用,似乎它在admin类中不起作用。
$ datagridMapper-&gt;添加(&#39; translations.title&#39);不会导致错误,并且有实体行,但<a> </a>
代码之间没有任何内容:
以防万一,这是我的Post,PostTranslation和PostAdmin课程:
/**
* Blog post
*
* @ORM\Table(name="post")
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
*/
class Post
{
use ORMBehaviors\Translatable\Translatable;
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="posts", fetch="EAGER")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
public function __construct() {}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setCategory(Category $category)
{
$category->addPost($this);
$this->category = $category;
}
public function getCategory()
{
return $this->category;
}
}
/**
* Post Translation
*
* @ORM\Entity
*/
class PostTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=255)
*/
private $content;
public function __construct() {}
/**
* Set title
*
* @param string $title
*
* @return post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
public function setContent($content)
{
$this->content = $content;
}
public function getContent()
{
return $this->content;
}
}
/**
* Class PostAdmin
*/
class PostAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('translations', 'a2lix_translations')
->add('category', 'entity', [
'class' => 'AppBundle\Entity\Category',
'property' => 'name'
])
;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('translations.title');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('translations.title');
}
}
答案 0 :(得分:0)
您需要将此代码添加到您的实体(而不是翻译代码):
public function __call($method, $arguments)
{
return \Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $method);
}
然后在configureListFields上,您可以放置'title'
或'translate.title'
。
此处有更多信息https://github.com/KnpLabs/DoctrineBehaviors#proxy-translations