Symfony2 Forms - 如何自动填充相关实体的字段?

Symfony2 Forms - How to populate a related Entity's field automatically?

我正在尝试按照 http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

上的 symfony2 动态表单修改教程进行操作

我的架构与他们在教程中使用的架构有点不同。我的包含以下关系:

国家(一对多)办公室
办公室(一对多)员工

当我编辑现有员工时,我希望它加载办公室所在的国家/地区作为默认选项,除了在办公室下拉列表中仅显示该国家/地区内的办公室(除非选择了另一个国家/地区) ,那么 jQuery 代码(不包括在内)应该相应地改变它)。

然而结果;是国家/地区字段仍然显示占位符值,而不是员工办公室的正确国家/地区。 (从好的方面来说,Office 下拉列表只显示该国家/地区的办公室,这意味着 $country->getOffices() 调用正在工作,所以我正在使用正确的 Country 对象,我似乎无法选择它默认情况下)。

我是否遵循了此处的最佳做法?是否缺少某些东西不允许我在相关实体的表单中设置值?

代码:

class EmployeeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('name')
        ->add('country', EntityType::class, array(
          'class'   =>  'AppBundle:Country',
          'mapped'  =>  false,
          'placeholder' =>  '=== Select a Country ===',
        ))
    ;

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) {
          $form = $event->getForm();

          // This will be the Employee entity
          $data = $event->getData();

          $office = $data->getOffice();
          $country = null === $office ? array() : $office->getCountry();

          $form->get('country')->setData($country); // I think this is what's not working properly.

          $form->add('office', EntityType::class, array(
              'class'       =>  'AppBundle:Office',
              'placeholder' =>  '=== Select an Office ===',
              'choices'     =>  $country->getOffices(),
          ));
        }
    );
}

我有机会快速阅读了您参考的教程 link,我认为您对错误发生的位置是正确的。

我认为(但我不确定)这可能是解决方法:

$office = $data->getOffice();
$offices = null === $office ? array() : $office->getCountry()->getOffices();

$form->add('office', EntityType::class, array(
  'class'       =>  'AppBundle:Office',
  'placeholder' =>  '=== Select an Office ===',
  'choices'     =>  $offices,
));

我只展示了你需要修改的相关部分。尝试一下,看看是否有帮助。

尝试修改事件数据:

class EmployeeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('country', EntityType::class, array(
                'class'   =>  'AppBundle:Country',
                'mapped'  =>  false,
                'placeholder' =>  '=== Select a Country ===',
            ))
        ;

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();

                // This will be the Employee entity
                $data = $event->getData();

                $office = $data->getOffice();

                // since the country field is not set as "multiple"
                // the data should not be an array but a string
                $country = null === $office ? '' : $office->getCountry();

                $data['country'] = $country(->getName()?);
                $event->setData($data); // may work

                $form->add('office', EntityType::class, array(
                    'class'       =>  'AppBundle:Office',
                    'placeholder' =>  '=== Select an Office ===',
                    'choices'     =>  $country->getOffices(),
                ));
            }
        );
    }
}