我正在尝试以1种形式保存2个类的实体,我已阅读this文章。我的代码是:
class MeetingType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('meetingDay', 'text', array('label' => 'Dzień zbiórki'))
->add('meetingTime', 'text', array('label' => 'Godzina zbiórki'))
->add('congregation', 'entity', array(
'class' => 'ViszmanCongregationBundle:Congregation',
'property' => 'name', 'label' => 'Zbór'
));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Viszman\CongregationBundle\Entity\Meeting'
));
}
/**
* @return string
*/
public function getName()
{
return 'viszman_congregationbundle_meeting';
}
}
另一种类型:
class CongregationType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$plDays = array('day1', 'day1', 'day1', 'day1', 'day1', 'day1', 'day1');
$builder
->add('name', 'text', array('label' => 'Name'))
->add('meetingDay', 'choice', array('label' => 'meeting day', 'choices' => $plDays))
->add('meetings','collection', array('type' => new MeetingType(), 'allow_add' => true))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Viszman\CongregationBundle\Entity\Congregation'
));
}
/**
* @return string
*/
public function getName()
{
return 'viszman_congregationbundle_congregation';
}
}
当我尝试渲染此表单时,我只会获得CongregationType
而不是MeetingType
表单。负责呈现表单的部分代码是:
<h1>Congregation creation</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
<h3>Meetings</h3>
<ul class="tags" data-prototype="{{ form_widget(form.meetings.vars.prototype)|e }}">
{{ form_widget(form.meetings) }}
</ul>
{{ form_end(form) }}