【问题标题】:Render Query results in a View using Twig in Symfony在 Symfony 中使用 Twig 渲染查询结果
【发布时间】:2020-04-22 19:15:44
【问题描述】:

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

我的存储库和我的查询:

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
    ]);
}

还有我的 Twig View:

    {% 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;
    }
}

【问题讨论】:

    标签: php symfony doctrine twig


    【解决方案1】:

    你真的很亲密。

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

    【讨论】:

    • 我试过了,但它没有显示“响应”,只有问题。但我没有收到任何错误
    • 您想从reponse 中显示哪些字段?在for reponse in cate.reponse 循环中,reponse 是一个对象,所以&lt;p&gt;{{ reponse }}&lt;/p&gt; 可能不是您想要的。你需要做类似&lt;p&gt;{{ reponse.reponse }}&lt;ps&gt;
    • 我想在我的 App\Entity\Reponse 中显示“响应”,它显示在我的转储(cate)中,但我不知道如何在我的 Twig 中显示它,{{ reponse.reponse }}给了我同样的结果
    • {{ reponse.reponse }} 应该可以工作。我想我只是注意到了其他问题。您没有在 repo 方法中正确加入您的协会。稍等,我会更新答案。
    • 我使用了您的答案,但结果相同,问题在这里,但没有“响应”的迹象。转储也不再显示“响应”
    猜你喜欢
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多