所以,我有3个实体。所有这些都是相关的,就像它显示在图片
我在控制器中有这个代码
/**
* After clicking the link at our index page we are getting to the show page
* where the user can pass the test
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
// Here we get out test's ID
$test = $em->getRepository('TestprojectTestManagerBundle:Test')->find($id);
// Then we get our questions information according to previously chosen test's ID
$questions = $em->getRepository('TestprojectTestManagerBundle:Question')->findBy(
array('test'=>$test),
array('question'=>'ASC')
);
// Here I tried to do the same with my answers, i.e. getting answers according to the current question
// But faild. $questions is all questions of our test. So, we get all answers of all questions of our test.
// Didn't figured out how to solve this.
$answers = $em->getRepository('TestprojectTestManagerBundle:Answer')->findBy(
array('questions'=>$questions),
array('answer'=>'ASC')
);
return $this->render('TestprojectTestManagerBundle:Test:show.html.twig', array(
'test'=>$test,
'questions'=>$questions,
'answers'=>$answers,
));
}
然后我正在尝试在show.html.twig中创建一个页面
<ul>
{% for question in questions %}
<li>
<p>{{ question.question }}</p>
{% if question.toa == 'select' %}
<form>
{% for answer in answers %}
<input type="radio" name="answerswer" value="{{ answer.answer }}">{{ answer.answer }}</input><br>
{% endfor %}
</form>
{% endif %}
{% if question.toa == 'check' %}
<form>
{% for answer in answers %}
<input type="checkbox" name="answerswer{{ answer.id }}" value="{{ answer.answer }}">{{ answer.answer }}</input><br>
{% endfor %}
</form>
{% endif %}
{% if question.toa == 'textfield' %}
<form>
{% for answer in answers %}
<input type="text" name="answerswer{{ answer.id }}"><br>
{% endfor %}
</form>
{% endif %}
</li>
{% endfor %}
</ul>
所以,我希望得到这样的东西:
Test1
Question 1
Q1.Answer 1
Q1.Answer 2
Question 2
Q2.Answer 1
Q2.Answer 2
但我得到了这个
Test1
Question 1
Q1.Answer 1
Q1.Answer 2
Q2.Answer 1
Q2.Answer 2
Question 2
Q1.Answer 1
Q1.Answer 2
Q2.Answer 1
Q2.Answer 2
如何根据答案相关的question_id显示答案?我还试图呼吁question_id并尝试在我的树枝模板中做出 if 语句。就像如果 question.id==answer.question_id然后显示答案。但它没有用。它说question_id不存在。虽然它存在于我的数据库中!在我的实体中有:
//in answer entity
/**
* @ORM'ManyToOne(targetEntity="Question", inversedBy="answerswers")
* @ORM'JoinColumn(name="question_id", nullable=false, referencedColumnName="id")
*/
protected $questions;
//in question entity
/**
* @ORM'OneToMany(targetEntity="Answer", mappedBy="questions", cascade={"all"}, orphanRemoval=true)
*/
protected $answers;
所以,我试着比较 question.id 并回答问题。但它说
在第 16 行的 TestprojectTestManagerBundle:Test:show.html.twig 中呈现模板("注意:类 Testproject''TestManagerBundle''Entity''Question 的对象无法转换为 int")时引发了异常。
我也尝试了 {% if question.answers == answer.questions %},它让渲染顺利,但它返回 false。
有人有什么想法吗?谢谢。附言对不起我的英语。
{% if question.id == answer.questions.id %}
你应该写
//in answer entity
/**
* @ORM'ManyToOne(targetEntity="Question", inversedBy="answerswers")
* @ORM'JoinColumn(name="question_id", nullable=false, referencedColumnName="id")
*/
protected $question; // not questions because its ManyToOne
然后在模板中它变成
{% if question.id == answer.question.id %}
通常,如果教义生成了实体,它会删除下划线。你试过 question.id == answer.questionId吗?而且,问题与数据库中的question_id无关。