Symfony2收藏。控制器中的关系

时间:2016-03-18 13:49:30

标签: php symfony collections

所以,我有3个实体。所有都是相关的,如它显示在 picture

我在控制器中有这个代码

/**
* 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="answer" value="{{ answer.answer }}">{{ answer.answer }}</input><br>
            {% endfor %}
            </form>

            {% endif %}

            {% if question.toa == 'check' %}

            <form>
            {% for answer in answers %}
            <input type="checkbox" name="answer{{ 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="answer{{ 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并尝试在我的twig模板中创建一个if语句。就像问题.id == answer.question_id然后显示answer.answer。但它没有用。它说question_id不存在。虽然它存在于我的数据库中! 在我的实体中有:

//in answer entity
/**
 * @ORM\ManyToOne(targetEntity="Question", inversedBy="answers")
 * @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和answer.questions。但它说

  

在TestprojectTestManagerBundle中测试模板(“通知:类Testproject \ TestManagerBundle \ Entity \ Question的对象无法转换为int”)期间抛出异常:测试:show.html.twig在第16行。

我也试过{%if question.answers == answer.questions%}并且它让渲染顺利,但它返回false。

有没有人有任何想法?谢谢。 附:对不起我的英文。

2 个答案:

答案 0 :(得分:0)

通常,如果doctrine生成实体,它会删除下划线。你有没有尝试过question.id == answer.questionId?而且,问题与数据库中的question_id无关。

答案 1 :(得分:0)

{% if question.id == answer.questions.id %}

你应该写

//in answer entity /** * @ORM\ManyToOne(targetEntity="Question", inversedBy="answers") * @ORM\JoinColumn(name="question_id", nullable=false, referencedColumnName="id") */ protected $question; // not questions because its ManyToOne

然后在模板中它变成

{% if question.id == answer.question.id %}