2013-08-29 77 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'标志。 –