php - 在 Symfony 中使用 Twig 渲染查询结果
问题描述
我正在尝试将我的查询结果显示到我的视图中。但是,我只能渲染该结果的一部分。
我的存储库和我的查询:
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
}
}
App\Entity\Reponse {#571 ▼
-id: 31
-id_question: 11
-reponse: "Appellation d'Origine Contrôlée"
-reponse_expected: 1
-question: null
}
这些是我的两个实体问题:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
*/
class Question
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
public $id_categorie;
/**
* @ORM\Column(type="string", length=255)
*/
private $question;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Reponse", mappedBy="question")
* @ORM\JoinColumn(nullable=false)
*/
private $reponse;
public function setReponse(Reponse $reponse) {
$this->reponse = $reponse;
return $this;
}
public function getReponse () {
return $this->reponse;
}
public function getId(): ?int
{
return $this->id;
}
public function getIdCategorie(): ?int
{
return $this->id_categorie;
}
public function setIdCategorie(int $id_categorie): self
{
$this->id_categorie = $id_categorie;
return $this;
}
public function getQuestion(): ?string
{
return $this->question;
}
public function setQuestion(string $question): self
{
$this->question = $question;
return $this;
}
}
回应:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ReponseRepository")
*/
class Reponse
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $id_question;
/**
* @ORM\Column(type="string", length=255)
*/
private $reponse;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $reponse_expected;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="reponse",)
*/
private $question;
public function setQuestion(Question $question) {
$this->question = $question;
return $this;
}
public function getQuestion () {
return $this->question;
}
public function getId(): ?int
{
return $this->id;
}
public function getIdQuestion(): ?int
{
return $this->id_question;
}
public function setIdQuestion(int $id_question): self
{
$this->id_question = $id_question;
return $this;
}
public function getReponse(): ?string
{
return $this->reponse;
}
public function setReponse(string $reponse): self
{
$this->reponse = $reponse;
return $this;
}
public function getReponseExpected(): ?int
{
return $this->reponse_expected;
}
public function setReponseExpected(?int $reponse_expected): self
{
$this->reponse_expected = $reponse_expected;
return $this;
}
}
解决方案
你真的很亲近。
首先,您手动加入响应实体,而不是使用您的关联。这弄乱了结果的格式。OneToMany
使用在实体上定义的关联更新您的存储库以加入,如下所示:
public function findIdQuestion($id) {
$query = $this->createQueryBuilder('question')
->andWhere('question.id_categorie = :id')
->leftjoin('question.reponse','reponse')
->addSelect('reponse')
->setParameter('id', $id)
->getQuery();
$results = $query->getResult();
return $results;
}
其次,您需要循环cate.reponse
而不是cate.question
{% for cate in affichage %}
<p>{{ dump(cate) }}</p>
<p>{{ cate.question}}</p>
{% for reponse in cate.reponse %}
<p>{{ reponse.reponse }}</p>
{% endfor %}
{% endfor %}
推荐阅读
- javascript - Vue cli3 块特定文件
- java - 从字符串中删除包含特定单词或部分单词的单词
- string - 根据字符串验证arraylist的内容
- rust - 有没有办法在 Rust 中定义多参数特征?
- windows - Windows 批处理:使用 PuTTY 和 sed 在特定位置的远程 Linux 文件中插入多行
- python - 使用 numpy 矢量化对大量对象状态进行操作的良好模式是什么?
- visual-studio-code - 有没有办法在视图面板中禁用命令发射按钮并在不满足条件时将其变灰?
- f# - Moq.FSharp.Extensions 中的 VerifyAction 问题
- python-3.x - 如何在python中使用re搜索[re]并跳到下一行的文本?
- ibm-information-server - 编译 V7R2 混淆