2013-08-29 143 views
0

我有這樣一個場景:多對多NULLABLE不允許

軌道多對多儀器

我創造這樣的形式:

$builder 
    ->add('instruments', 'entity_hidden', array(
          'attr' => array('class' => 'one_req'), 
          'label' => 'form.instrument', 
          'translation_domain' => 'label', 
          'required' => false, 
          'class' => 'Acme\DemoBundle\Entity\Instrument' 
           )) 

「hidden_​​entity」是一個自定義的變壓器因爲你可以在這裏找到:gist

雖然我認爲這不是問題。

問題是,用戶甚至可能不會選擇任何儀器(例如其他指定的儀器,如"required" => false)。

當我去拯救我返回此錯誤:

Neither the property 'instruments' nor one of the methods 'setInstruments()', 
'__set()' or '__call()' exist and have public access in 
class 'Acme\DemoBundle\Entity\SoundtrackVersion'. 

我不明白爲什麼..

我在哪裏做錯了嗎?

+0

你確定在'Acme \ DemoBundle \ Entity \ SoundtrackVersion'中有一個'setInstruments'方法嗎?確保你運行了'php app/console doctrine:generate:entities' – Touki

+1

沒有setInstrument不存在,但應該有! 是一個關係ManyToMany和教條:生成:實體不創建設置方法,但只添加方法。 然後我不會沒有任何價值,我不明白爲什麼它必須使用一套方法.. – Lughino

回答

1

這聽起來很明顯,但錯誤不能說謊。檢查Acme\DemoBundle\Entity\SoundtrackVersion實體是否有財產instruments和方法setInstruments()getInstruments()

+0

我也在上面的評論中寫道:沒有setInstrument不存在,但應該有!有一種方法addInstrument,因爲它是ManyToMany的關係。然後我不會沒有任何價值,我不明白爲什麼它必須使用一套方法.. – Lughino

+0

好吧,我進入設置方法:'/ ** *設置儀器 * * @參數字符串$儀器 * @return SoundtrackVersion */ 公共功能setInstruments($ instruments) { $ this-> instruments = $ instruments; return $ this; }現在不再給出錯誤,並且不會在數據庫中創建任何記錄。 我可以解釋爲什麼在ManyToMany關係中使用此方法? – Lughino

+2

在[documentation](http://symfony.com/doc/current/cookbook/form/form_collections.html)中解釋了這個問題的細節。看看'by_reference'標誌。 –