将表单的 foreach 循环传递给 Twig

时间:2021-06-16 08:43:05

标签: forms foreach twig symfony5

我正在创建一个编辑页面,其中的表单数量与查询结果的数量一样多。因此,在查询之后,我创建了一个 foreach 循环,将每个“结果”传递给一个表单,然后将其全部传递给我的 Twig。

$repo = $this->getDoctrine()->getRepository(Classement::class);
    
    $q = $repo->createQueryBuilder('c');
    $q->select('c', 'h')
      ->join('c.clubHistos', 'h')
      ->where('c.country = '.$country)
      ->where('c.season = '.$season);
    $result = $q->getQuery()->getResult();


    foreach ($result as $table) {
        $table  = $table;
        
        $form = $this->createForm(ClassementType::class, $table);
    }
    
    return $this->render('back/editDivisionTables.html.twig', array(
        'form' => $form->createView(),
        'table' => $table
    ));

问题是我只在我应该拥有所有表格时才得到最后一张表格。我在循环中做了一个 dump($form) ,一切似乎都很好。所以问题要么出在我的 return 语句中,要么出在我的 Twig 中。

{{ form_start(form) }}
        <div class="season-table-info">
            <div class="country">
                {{ form_row(form.country) }}
            </div>
            <div class="division">
                {{ form_row(form.division) }}
            </div>
            {{ form_row(form.season) }}
        </div>

        <table id="dynamic-club-table">
            <thead></thead>

            <tbody id="dynamic-club-table-body" data-row-prototype="{{ formMacro.form(form.clubHistos.vars.prototype)|e('html_attr') }}">
                {% for clubHistos in form.clubHistos %}
                    {{ formMacro.form(clubHistos) }}
                {% endfor %}
            </tbody>
        </table>
{{ form_end(form) }}    

我尝试了 this solution,但没有成功。

0 个答案:

没有答案