首页 > 解决方案 > 在 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;
    }
}

标签: phpsymfonydoctrinetwig

解决方案


你真的很亲近。

首先,您手动加入响应实体,而不是使用您的关联。这弄乱了结果的格式。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 %}

推荐阅读