做一个列表字段编辑时,这个领域是采用奏鸣曲项目的Symfony

问题描述:

我的实体做一个列表字段编辑时,这个领域是采用奏鸣曲项目的Symfony

/** 
* @ORM\ManyToOne(targetEntity="Estat", inversedBy="temes") 
*/ 
private $estat; 


public function setEstat(\Ncd\ForumBundle\Entity\Estat $estat = null) 
{ 
    $this->estat = $estat; 

    return $this; 
} 

我管理一个many_to_one型

protected function configureListFields(ListMapper $listMapper) 
{ 

    //$estats=$this->getEstatsPossibles()->toArray(); 
    $estats=array(); 
    foreach($this->getEstatsPossibles() as $estat) 
    { 
     $estats[$estat->getId()]=$estat->getNom(); 
    } 

    $listMapper 
     ->add('estat', 'choice',['editable' => true,'choices'=> $estats]) 

我想打在ESTAT场编辑列表网格。这样做,我得到了它的可编辑性,一个组合框出现,但是当我选择一个选项时,我得到一个异常,因为我的实体的setEstat函数不会收回Estat实体,而是一个字符串(数组的键)。

试图

->add('estat', 'many_to_one',['editable' => true,'choices'=> $estats]) 

只显示一个链接到实体没有任何可能改变。

可能吗?

等待一个更好的和更清洁的解决方案我心中已经在我的实体解决了这个注入一个EntityManager这个答案的解决方案如下: Get entityManager inside an Entity

然后,在我的实体我已经改变了setEstat功能:

public function setEstat($estat = null) 
{ 
    if (is_object($estat) && get_class($estat)=='Ncd\ForumBundle\Entity\Estat') 
    { 
     $this->estat=$estat; 

    } else { 
     $estat_o=$this->em->getRepository('Ncd\ForumBundle\Entity\Estat')->find((int)$estat); 
     $this->estat = $estat_o; 
    } 


    return $this; 
}