php - 在 TWIG 中包含标签

标签 php symfony twig

我正在尝试在问题答案 Twig 页面中使用包含标签来包含资源 Twig 页面的实体(queId),但我收到错误:

questionanswer.html.twig:

        {% for entity in entities %}
        <tr>
        <td><a href="{{ path('questionanswer_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
        <td>{{ entity.question }}</td>
        <td>{{ entity.answer }}</td>
        <td>
        {% if entity.getResource is empty %}
        <p> </p>
        {% else %}                        
        <a href="{% include 'SymfonySymfonyBundle:resource:index.html.twig' with { 'id': entity.queId } %}">Yes</a>
        {% endif %}
        </td>

无法将外键 (queId) 从资源实体返回到 Questionanswer twig 中。

问题答案实体:

            /**
             * @var ArrayCollection
             * 
             * @ORM\OneToMany(targetEntity="resource", mappedBy="queId")
             */

            private $resources;

            /**
             * Get resources
             *
             * @return \Doctrine\Common\Collections\Collection
             */
            public function getResources() {
            return $this->resources->toArray();

资源实体:

            /**
             * @var integer
             * @ORM\ManyToOne(targetEntity="questionanswer", inversedBy="resources")
             * @ORM\JoinColumn(name="que_id", referencedColumnName="id")
             */
            private $queId;

最佳答案

该方法在您的对象中名为 getResources,但您在模板中使用 getResource(注意单数)

关于php - 在 TWIG 中包含标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31331895/

相关文章:

php - 在 Twig 中解码 JSON

symfony - FOS UserBundle - 覆盖 FormFactory

Twig:删除表单每个复选框字段周围的 div

php - 是否可以从 Twig 调用实体的 getter 方法之一?

Javascript document.getElementById() 在提交按钮后不起作用

php - 使用外键插入 PDO 失败

php - 不同的表字段标签

php - 当所有对象都属于同一类型时,我可以省略 PHP in_array() 中的 strict 参数吗?

php - 与 Symfony 表单的关注点分离

php - Symfony2 DATEDIFF 函数