2013-03-26 90 views
1

我在使用REST API的Symfony2應用程序(沒有Doctrine實體)。Symfony2 ObjectChoiceList選擇正確的選項

我有一個表單類型: ($這個 - > regionService是服務從API檢索所有可用的地區)

$regionChoiceList = new ObjectChoiceList($this->regionService->getAll(), 'name', array(), null, 'id'); 

$builder->add('region', 'choice', array(
       'error_bubbling' => true, 
       'required' => false, 
       'choice_list' => $regionChoiceList 
      )) 

創建新實體的所有工作正常。當我嘗試編輯這種類型的對象時,我沒有選擇正確的選項。 序列化的對象是正確的,留下的JSON序列化響應對象的例子:

Object: 
    "name" = "blahblah" 
    "etc..." 
    "region"(Region Object): 
    "name" = "name region" 
    "id" = identifier region 

回答

1

可以更換通過與包含在對象實體的一個RegionService返回列表中的地區。

看來,實例必須是相同的,使symfony選擇corret選項。

+0

RegionService返回相同的對象 – Mauro 2013-04-02 12:06:30

+0

我有完全相同的問題:我期望表單根據'$ valuePath'屬性的值選擇正確的選項(在本例中爲'id'),但它實際上會比較對象,因此具有相同屬性的兩個不同實例將不匹配。 – 2013-08-17 18:19:14

+0

I.e.如果你的'var_dump($ this-> regionService-> getAll())'和'var_dump'表單數據,你可能會得到'object(Region)#42(4){[「id」:protected] => int( 1)'和'object(Region)#314(4){[「id」:protected] => int(1)':即使它們具有相同的屬性,它們也是不同的實例。 – 2013-08-17 18:20:49