[Symfony中使用Twig的渲染查询结果在视图中

问题描述 投票:0回答:1

我正在尝试将查询结果显示到视图中。但是,我只能渲染该结果的一部分。

带有查询的我的存储库:

public function findIdQuestion($id) {
    $query = $this->createQueryBuilder('question')
            ->andWhere('question.id_categorie = :id')
            ->leftjoin('App\Entity\Reponse','reponse','WITH','reponse.id_question = question.id')
            ->addSelect('reponse')
            ->setParameter('id', $id)
            ->getQuery();

        $results = $query->getResult();

        return $results;

  }

这里是我的控制器:

public function play(Request $request) {

    $id = $request->query->get('id');

    $cat = $this->repository->findIdQuestion($id);

    dump($cat);

    return $this->render('quiz_select.html.twig', [
        'affichage' => $cat
    ]);
}

和我的树枝视图:

    {% block body %}
<h1>Questions</h1>
    {% for cate in affichage %}
        <p>{{ dump(cate) }}</p>
        <p>{{ cate.question }}</p>      
    {% endfor %}
{% endblock %}

这很好用,但是我也需要显示每个“响应”。

我尝试过:

    {% for cate in affichage %}
        <p>{{ dump(cate) }}</p>
        <p>{{ cate.question }}</p>
            {% for reponse in cate.question %}
                <p>{{ reponse }}</p>
            {% endfor %}      
    {% endfor %}

但是它什么也没显示我也尝试过:

    {% for cate in affichage %}
        <p>{{ dump(cate) }}</p>
        <p>{{ cate.question }}</p>
        <p>{{ cate.reponse }}</p>     
    {% endfor %}

但是我得到那个错误:

Doctrine \ ORM \ PersistentCollection类的对象无法转换为字符串

这是我的转储(cate)中的内容:

    App\Entity\Question {#563 ▼
  -id: 21
  +id_categorie: 3
  -question: "Que signifie le verbe Enrêner ?"
  -reponse: Doctrine\ORM\PersistentCollection {#512 ▼
    -snapshot: []
    -owner: App\Entity\Question {#563}
    -association: array:15 [ …15]
    -em: Doctrine\ORM\EntityManager {#265 …11}
    -backRefFieldName: "question"
    -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#518 …}
    -isDirty: false
    #collection: Doctrine\Common\Collections\ArrayCollection {#568 ▶}
    #initialized: false
  }
}
php symfony model-view-controller doctrine twig
1个回答
0
投票

这可能会有所帮助。如果没有,请提供两个实体的代码。

$qb = $this->entityManager->createQueryBuilder();
    $qb
        ->select('r', 'q')
        ->from('reponse', 'r')
        ->leftJoin('reponse.id_question', 'q')
        ->andWhere('q.id_categorie = :id')
        ->setParameter('id', $id);

    return $qb->getQuery()->getResult();
© www.soinside.com 2019 - 2024. All rights reserved.