Pre select 在 Symfony2 中带有现成实体的表单

Pre select Form with ready entity in Symfony2

想在 Symfony 中预 select 我的表单。 我用表单生成器来做。除了 child table 不保存外,它都有效。

我的发票类型

    public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder
            ->add('invoiceNumber', 'hidden', array('label' => ''))
            ->add('date', 'date', array('label' => 'Field', 'data' => new \DateTime("now")))
            ->add('PaidPrice', 'money', array('label' => 'Bereits bezahlt', 'attr' => array('class' => '')))
            ->add('invoicepos', 'collection', array(
                    'type' => new InvoiceposType(),
                    'allow_add'    => true,
                    'allow_delete' => true,
                    'cascade_validation' => true,
                    'by_reference' => true,
                ))
    ;
}

我的 invoicepos 类型

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('pos', 'text', array('label' => '', 'attr' => array()))
        ->add('quantity', 'hidden')
        ->add('price', 'hidden')
        ->add('tax', 'hidden')
    ;
}

我的控制器启动表单

public function newAction($id) {
    $em->persist($entInvoice);
    //$em->flush($entInvoice); //works perfect, but i dont want to save that, just pre select for the form
    $form = $this->createCreateForm($entInvoice);
    return array(
        'entity' => $entInvoice,
        'form' => $form->createView(),
    );
}

提交表单时的代码

public function createAction(Request $request) {
    $entity = new Invoice();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('pspiess_letsplay_invoice_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
    );
}

数据是有的,但没有关系!

我做错了什么? 感谢您的帮助。

找到了解决我的问题的方法。 在我刷新之前我添加了子实体...

foreach ($entity->getInvoicepos() as $entInvoice) {
  $entity->addInvoicepos($entInvoice);

}