在twig文件中渲染SQL结果

时间:2014-07-10 10:06:10

标签: symfony twig

如何在树枝中渲染以下查询的结果?

$q = $em->createQuery('
        SELECT mark, student.studentName FROM DemoTemplateBundle:TblMarkDetails mark, DemoTemplateBundle:TblStudentDetails student
        WHERE student.id = mark.studentId'
);
$marks = $q->getArrayResult();

表格内容

 Table tbl_mark_details                ||   tbl_student_details
 ---------------------------------------------------------------
 id | student_id    | exam_id  | score ||   id | student name
 1  |1              | 1        |10     ||   1  | Student 1
 2  |2              | 1        |5      ||   2  | Student 2
 3  |2              | 2        |25     ||

我尝试了以下代码,但第三行没有学生姓名

{% set i=0 %}
{% for mark in marks %}
  {% if i%2 == 0 %}
  <tr>
   <td>{{ mark.score  }}</td>
   <td>{{ mark.examId  }}</td>
  {% else %}
    <td>{{ mark.studentName  }}</td>
  </tr>
  {% endif %}
  {% set i = i+1 %}
{% endfor %}

由于

Vishnu V

1 个答案:

答案 0 :(得分:0)

你应该这样试试:

    $q = $em->createQuery('
    SELECT mark, student FROM DemoTemplateBundle:TblMarkDetails mark,     DemoTemplateBundle:TblStudentDetails student
    WHERE student.id = mark.studentId'
    );
    $marks = $q->getResult();

你的树枝没事......