一种形式的多个实体 Symfony

Multiple entity in one form Symfony

我有一个实体 "ticket",我想创建一个包含许多实体的表单。

所以我创建了一个这样的 FormType :

class TicketType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('quantity');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => null,
        ));
    }

    public function getName()
    {
        return 'event';
    }
}

这是我的控制器:

    $clientTickets = array();

    for ($i = 0; $i < 5; $i++) {
        clientTickets[] = new Ticket();
    }

        $formTickets = $this->createForm(new TicketType(), array('tickets' => $clientTickets));

        if ($request->getMethod() == 'POST') {
            $formTickets->handleRequest($request);
            for ($i=0; $i < $formTickets.size(); $i++) { 

                $em->persist($ticket[$i]);
             }
        }

最后是我的树枝:

{{ form_start(form, {'attr': {'class': 'form-horizontal', 'id': 'msform', 'name': 'myform', 'method': 'POST' }}) }}

                                          {% for clientTicket in form.tickets %}

                                            {{ form_widget(form.quantity) }}

                                          {% endfor %}

                                              {{ form_rest(form) }}

                                          {{ form_end(form) }}

当我进入该页面时,出现此错误:

Method "tickets" for object "Symfony\Component\Form\FormView" does not exist in YourBundle:folder:view.html.twi at line

为什么我做错了?你有解决办法吗?

更新: 我认为你需要做这样的事情:

{% for i in 0..form|length %}
    {{ form[i].method }}
{% endfor %}

您可能需要这样做:

让我们输入以下表单:

class TicketType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder - > add('field1', 'text');
        $builder - > add('field2', 'text');
        $builder - > add('field3', 'text');
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver - > setDefaults(array(
            'data_class' = > null,
        ));
    }
    public function getName() {
        return 'event';
    }
}

然后在你的控制器中你需要有这样的东西:

public function testAction( )
{
    $clientTickets = array(
        'tickets' => array(
            array(
                'field1' => 'aaa',
                'field2' => 'aaa',
                'field3' => 'aaa'
            ),
            array(
                'field1' => 'bbb',
                'field2' => 'bbb',
                'field3' => 'bbb'
            ),
            array(
                'field1' => 'ccc',
                'field2' => 'ccc',
                'field3' => 'ccc'
            )
        )
    );

    $formTickets = $this->createFormBuilder( $clientTickets )
                        ->add( 'tickets', 'collection', array( 'type' => new TicketType() ) )
                        ->getForm();

    if ($formTickets->handleRequest( $this->getRequest() )->isValid()) {
        $data = $formTickets->getData();

        var_dump( $data );
    }

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

你的树枝看起来像这样:

{{ form_start(form) }}

<ul>
    {% for clientTicket in form.tickets %}
        <li>{{ form_row(clientTicket.field1) }} {{ form_row(clientTicket.field2) }} {{ form_row(clientTicket.field3) }}</li>
    {% endfor %}
</ul>

{{ form_rest(form) }}
{{ form_end(form) }}

我认为这段代码是不言自明的。希望对你有帮助