2016-02-01 79 views
1

我使用「Knp Doctrine2 Behaviors」進行實體翻譯,「A2LiX Translation Form」使用Sonata Admin表單翻譯實體。一切工作正常。但我不知道我應該在Admin類的「configureDatagridFilters」和「configureListFields」方法中放置什麼?當然,例如,$ datagridMapper-> add('title');不起作用,因爲它是我的「Post」實體的可翻譯字段,它位於PostTranslation類中,但Admin類是Post實體。 Post實體中的__call方法沒有幫助,它似乎在管理類中不起作用。如何在Sonata Admin中使用可翻譯字段?

$ datagridMapper-> add('translations.title');不會導致錯誤,有一行實體,但<a> </a>標籤之間沒有任何東西:

enter image description here

以防萬一,這裏是我的帖子,翻譯後和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

您需要將此代碼添加到您的實體(沒有翻譯的):

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

相關問題