Symfony 从数组集合中检索数据
Symfony retrieve data from Array Collection
在 Symfony 中,我使用查询从数据库中检索所有问题及其相关答案。在树枝中使用 for
我试图在 table 中逐行显示所有问题和相关答案。目前我只能访问和显示问题,但不能访问和显示答案。
我的实体 Questions and Answers
之间的关系是(一对多),因此在检索问题时,也会检索到答案。使用转储,我可以看到答案放在数组集合中。
尝试使用我的 Question
实体中的 getAnswers()
函数,使用另一个查询从数据库中仅获取一个问题,我能够使用以下语句从数组集合中获取答案:
$answer = $question->getAnswers()->getValues();
使用此语句,我尝试使用控制器中的波纹管代码获取所有问题的答案,但出现此错误:
Error: Call to a member function getAnswers() on a non-object
我相信这是因为查询 returns 多个问题而不仅仅是一个。
如何从 Array Collection 中获取每个问题的答案?在此先感谢您的帮助。
控制器代码:
$question = $this->getDoctrine()
->getRepository('QuizBundle:Question')
->findByIdJoinedToCategory();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $question));
问题库:
public function findByIdJoinedToCategory()
{
$query = $this->getEntityManager()
->createQuery(
'SELECT a, q FROM QuizBundle:Question a
JOIN a.answers q');
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
树枝代码:
{% for item in data %}
<tbody>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.image }}</td>
<td>{{ item.question }}</td>
<td></td>
<tr>
<tbody>
{% endfor %}
转储查询数据的屏幕截图:
使用 Twig 并根据您的学说查询,您可以像这样显示您的 questions/answers :
{% for item in data %}
<tbody>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.image }}</td>
<td>{{ item.question }}</td>
{% for answer in item.answers %}
{{ answer.id }}
{% endfor %}
<tr>
<tbody>
{% endfor %}
顺便说一句,如果两个实体 Question 和 Answer 之间存在关系,并且您想显示所有问题及其关联的答案,您可以直接这样做:
在你的控制器中:
$questions = $this->getDoctrine()
->getRepository('QuizBundle:Question')->findAll();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $questions));
在你的 Twig 中:
{% for question in data %}
<tbody>
<tr>
<th scope="row">{{ question.id }}</th>
<td>{{ question.image }}</td>
{% for answer in question.answers %}
{{ answer.id }}
{% endfor %}
<tr>
<tbody>
{% endfor %}
在 Symfony 中,我使用查询从数据库中检索所有问题及其相关答案。在树枝中使用 for
我试图在 table 中逐行显示所有问题和相关答案。目前我只能访问和显示问题,但不能访问和显示答案。
我的实体 Questions and Answers
之间的关系是(一对多),因此在检索问题时,也会检索到答案。使用转储,我可以看到答案放在数组集合中。
尝试使用我的 Question
实体中的 getAnswers()
函数,使用另一个查询从数据库中仅获取一个问题,我能够使用以下语句从数组集合中获取答案:
$answer = $question->getAnswers()->getValues();
使用此语句,我尝试使用控制器中的波纹管代码获取所有问题的答案,但出现此错误:
Error: Call to a member function getAnswers() on a non-object
我相信这是因为查询 returns 多个问题而不仅仅是一个。 如何从 Array Collection 中获取每个问题的答案?在此先感谢您的帮助。
控制器代码:
$question = $this->getDoctrine()
->getRepository('QuizBundle:Question')
->findByIdJoinedToCategory();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $question));
问题库:
public function findByIdJoinedToCategory()
{
$query = $this->getEntityManager()
->createQuery(
'SELECT a, q FROM QuizBundle:Question a
JOIN a.answers q');
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
树枝代码:
{% for item in data %}
<tbody>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.image }}</td>
<td>{{ item.question }}</td>
<td></td>
<tr>
<tbody>
{% endfor %}
转储查询数据的屏幕截图:
使用 Twig 并根据您的学说查询,您可以像这样显示您的 questions/answers :
{% for item in data %}
<tbody>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.image }}</td>
<td>{{ item.question }}</td>
{% for answer in item.answers %}
{{ answer.id }}
{% endfor %}
<tr>
<tbody>
{% endfor %}
顺便说一句,如果两个实体 Question 和 Answer 之间存在关系,并且您想显示所有问题及其关联的答案,您可以直接这样做:
在你的控制器中:
$questions = $this->getDoctrine()
->getRepository('QuizBundle:Question')->findAll();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $questions));
在你的 Twig 中:
{% for question in data %}
<tbody>
<tr>
<th scope="row">{{ question.id }}</th>
<td>{{ question.image }}</td>
{% for answer in question.answers %}
{{ answer.id }}
{% endfor %}
<tr>
<tbody>
{% endfor %}