Symfony2.3,1种形式的关系实体

时间:2015-07-16 14:03:16

标签: php symfony twig symfony-2.3

我正在尝试以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) }}

1 个答案:

答案 0 :(得分:0)

如果我没记错的话,实体表单字段用于引用现有实体,而不是创建新实体。

这不是您所需要的,您需要的是嵌入式表格,请查看the symfony book