我正在做一个大学项目,我想参加所有学生的出勤。我创建了一个包含 3 个字段的模型,即日期、当前(布尔值)和 student_id。现在,当我尝试从中生成表单时,它只会显示这 3 个字段。但是我想要班里的所有学生。所以我为学生创建了一个循环并创建了一个出勤对象数组。现在我被卡住了,我不知道如何将它们传递给我的 TWIG 文件,而且我也很困惑这是否是正确的方法。这是我的模型和控制器代码
形式
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';
}
}
控制器
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,
));
}
我怎样才能呈现表单,它会用一个单独的复选框向我显示所有学生?
HTML.TWIG
{% 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 %}