2017-08-09 19 views
1

我的實體做一個列表字段編輯時,這個領域是採用奏鳴曲項目的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]) 

只顯示一個鏈接到實體沒有任何可能改變。

可能嗎?

回答

-1

等待一個更好的和更清潔的解決方案我心中已經在我的實體解決了這個注入一個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; 
} 
相關問題