Erstellen von Wiederholungsfeldern in meinem Formular Symfony2

Ich arbeite an einem College-Projekt, bei dem ich die Anwesenheit aller Studenten übernehmen möchte. Ich habe ein Modell mit 3 Feldern i, e date, present (boolean) und student_id erstellt. Wenn ich jetzt versuche, ein Formular daraus zu generieren, werden mir nur diese 3 Felder angezeigt. Ich möchte jedoch alle Schüler der Klasse. Also habe ich eine Schleife für Schüler erstellt und eine Reihe von Anwesenheitsobjekten erstellt. Jetzt stecke ich fest, ich weiß nicht, wie ich sie an meine TWIG-Datei übergeben kann, und ich bin auch verwirrt, ob dies der richtige Weg ist. Hier ist mein Modell und Controller-Code

BILDEN
<code>namespace College\StudentBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class StudentAttendanceType extends AbstractType
{
   public function buildForm(FormBuilder $builder, array $options)
   {
     $builder
        ->add('date')
        ->add('present')
    ;
   }

   public function getName()
   {
     return 'college_studentbundle_studentattendancetype';
   }
}
</code>
REGLER
<code>public function takeAttendanceAction($Department_Id)
{
    $students = $this->getDoctrine()
    ->getRepository('CollegeStudentBundle:Student')
    ->findAll($Department_Id);

    foreach($students as $key => $student){

        $attendance[$key] = new StudentAttendance();
        $form[$key] = $this->createForm(new StudentAttendanceType(), $attendance[$key]);
    }

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {

        $form->bindRequest($request);
        if ($form->isValid()) {

            $em = $this->getDoctrine()
            ->getEntityManager();
            $em->persist($attendance);
            $em->flush();
            return $this->redirect($this->generateUrl('CollegeStudentBundle_index', array('id' => $Department_Id)));
        }
    }
    return $this->render('CollegeStudentBundle:StudentAttendance:take-attendance.html.twig', array(
            'form' => $form->createView(), 'department' => $Department_Id, 'students' => $students,
    ));
}
</code>

Wie kann ich das Formular so rendern, dass mir alle Schüler mit einem separaten Kontrollkästchen angezeigt werden?

HTML.TWIG
<code>{% block body  %}
<form  action="{{ path('CollegeStudentBundle_take_attendance',{'id':department} ) }}" method="post" {{ form_enctype(form) }} name="acadimics-form" id="acadimics-form" >

{{ form_errors(form) }}
{{ form_row(forms[0].date) }}

<table id="mytabs" border="1" cellpadding="5" cellspacing="2" width="100%" >
   <tr>
    <th> Enrolment No. </th>
    <th> Student's Name </th>
    <th> Present </th>
   </tr>
  {% for student in students %}
    <tr>
       <td> {{ student.enrolmentNo }} </td>
       <td> {{ student.firstname }} {{ student.lastname }} </td>
       <td> {{ form_row(form.present) }} </td>
        </tr>
  {% endfor %}
  {{ form_rest(form) }}
</table>

<input type="submit" value="Take Attendance"  />
</form>
  </div>

{% endblock %}
</code>

Antworten auf die Frage(3)

Ihre Antwort auf die Frage