Symfony:Form Builder 没有获得 OneToMany 输入

Symfony: Form Builder doesn't get OneToMany inputs

所以我在两个实体之间有 OneToMany 关系,我已经创建了相关的表格,但似乎 FormBuilder,在发送数据后,没有看到 children 实体。
Parent class:

class Parent{
   /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */

   private $id;

   /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
   private $name;

   /**
     * @ORM\OneToMany(targetEntity="Child",mappedBy="parent",cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=false)
     */
   private $children;
}

Child class:

class Child{
    /**
      * @var int
      *
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
    private $id;
     /**
      * @var string
      *
      * @ORM\Column(name="name", type="string", length=255)
      */
    private $name;
    /**
     * @ORM\ManyToOne(targetEntity="Parent", inversedBy="children")
     */
    private $parent;
}

这是 FormTypes:

class ParentType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name')
            ->add('children', CollectionType::class, array(
            'entry_type' => ChildType::class,
            'allow_add' => true

        ));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Bundle\Entity\Parent'
        ));
    }

}

Child 格式类型:

class ChildType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Bundle\Entity\Child'
        ));
    }
}

之后,我在控制器中创建了我的表单,如下所示:

function saveAction(Request $request){
    $parent = new Parent();
    $form = $this->createForm('Bundle\Form\ParentType', $parent);
    $form->handleRequest($request);
    if ($form->isSubmitted()) {
            //--after submitted the form I just want to display the children
            die(var_dump($parent->getChildren()));


        }
}

当然,我正在使用 symfony 的 FormBuilder 提供的 data-prototype 中的 jquery 创建 children,但是表格看不到它们!
我在这里错过了什么?

可能是因为您的 'setters' 没有被调用。

尝试添加 by_reference 并将其设置为 false,如下所示:

class ParentType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name')
            ->add('children', CollectionType::class, array(
            'entry_type' => ChildType::class,
            'allow_add' => true,
            'by_reference' => false

        ));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Bundle\Entity\Parent'
        ));
    }

}

我没有时间实际测试您的代码。但是,您不必在 child 表单中输入 parent。所以你的 child formType 应该是这样的:

class ChildType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name')      ;

    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Bundle\Entity\Child'
        ));
    }
}

希望对您有所帮助。