duan02468 2017-09-29 12:46
浏览 33
已采纳

如何以递归方式在树枝中显示注释

I have problem with comments display in twig. They are visible if I list them all but I need them to be nested.

This is entity, I thought it should be referenced like this:

/**
 * @var \AppBundle\Entity\Comment
 * 
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Comment")
 * @ORM\JoinColumn(name="parentId", referencedColumnName="id")
 */
private $parentId;

Controller is simple, fetch all comments from db and returns array( with, and without parentId) Following some instruction i added this to main twig file:

<!-- Comments and omments with parentId -->
{% include 'front/main/comments-main.html.twig' with {'commments':comments} %}

Listing all comments works. But in included twig it seems this peace of code

{% if comment.parentId != null %}
            {% set children = [] %}
            {% set children = children|merge([ comment ]) %}
            {% include 'front/main/comments-main.html.twig' with {'comments':children} %}
        {% endif %}

does not work. If I echo something it is displayed in the right place, under comment with that id. But with that lines inside if not. Page load very slow and never ends. Like infinite loop. What I am doing wrong?

  • 写回答

1条回答 默认 最新

  • duanjinchi1982 2017-09-30 13:00
    关注

    Well, I did it. It was problem with infinite loop but because I was dealing with parentId, and I was following instructions of a someone who was dealing with object.children. So I had to make relation to be Many to One self-directional

    // ...
    /**
     * One Category has Many Categories.
     * @OneToMany(targetEntity="Category", mappedBy="parent")
     */
    private $children;
    
    /**
     * Many Categories have One Category.
     * @ManyToOne(targetEntity="Category", inversedBy="children")
     * @JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;
    // ...
    
    public function addChild(Comment $child) {
       $this->children[] = $child;
       $child->setParentId($this);
    }
    public function __construct() {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }
    

    (from http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-self-referencing) And to make small change in controler to set children.Before flush()

            $parent = $comment->getParentId();
            $parent->addChild($comment);
    

    And in twig, in subtemplate I already had, inside the loop

    <div>
        {% if comment.children is defined %}
            {% include 'front/main/comments-main.html.twig' with {'comments':comment.children} %}
        {% endif %}
    </div>
    

    Hope to save some minutes to someone, I lost hours!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数