Symfony3 - 树渲染

时间:2017-03-29 08:20:54

标签: symfony twig

我从symfony开始,我尝试创建一个包含评论的博客。注释可能是对另一个注释的响应:在这种情况下,它稍微向右移动以显示注释树。评论的子级别不能超过3个。这是我的评论实体:

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * Comment
 *
 * @ORM\Table(name="comment")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CommentRepository")
 */
class Comment
{
    /**
     * Number of levels comments
     */
    const NUM_LEVELS = 3;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Comment", inversedBy="childs")
     * @ORM\JoinColumn(nullable=true)
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Comment", mappedBy="parent")
     * @ORM\JoinColumn(nullable=true)
     */
    private $childs;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Article", inversedBy="comments")
     * @ORM\JoinColumn(nullable=false)
     *
     */
    private $article;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="author", type="string", length=255)
     */
    private $author;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    /**
     * @var boolean
     * @ORM\Column(name="signaled", type="boolean")
     */
    private $signaled = false;

    /**
     * @var int
     * @ORM\Column(name="level", type="integer")
     */
    private $level = 0;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text")
     */
    private $content;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set author
     *
     * @param string $author
     *
     * @return Comment
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return string
     */
    public function getAuthor()
    {
        return $this->author;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return Comment
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set content
     *
     * @param string $content
     *
     * @return Comment
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return string
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Set article
     *
     * @param \AppBundle\Entity\Article $article
     *
     * @return Comment
     */
    public function setArticle(\AppBundle\Entity\Article $article)
    {
        $this->article = $article;

        return $this;
    }

    /**
     * Get article
     *
     * @return \AppBundle\Entity\Article
     */
    public function getArticle()
    {
        return $this->article;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->childs = new \Doctrine\Common\Collections\ArrayCollection();
        $this->date = new \Datetime();
    }

    /**
     * Set parent
     *
     * @param \AppBundle\Entity\Comment $parent
     *
     * @return Comment
     */
    public function setParent(\AppBundle\Entity\Comment $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return \AppBundle\Entity\Comment
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Add child
     *
     * @param \AppBundle\Entity\Comment $child
     *
     * @return Comment
     */
    public function addChild(\AppBundle\Entity\Comment $child)
    {
        $this->childs[] = $child;
        $child->setParent($this);

        return $this;
    }

    /**
     * Remove child
     *
     * @param \AppBundle\Entity\Comment $child
     */
    public function removeChild(\AppBundle\Entity\Comment $child)
    {
        $this->childs->removeElement($child);
    }

    /**
     * Get childs
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChilds()
    {
        return $this->childs;
    }

    /**
     * Set signaled
     *
     * @param boolean $signaled
     *
     * @return Comment
     */
    public function setSignaled($signaled)
    {
        $this->signaled = $signaled;

        return $this;
    }

    /**
     * Get signaled
     *
     * @return boolean
     */
    public function getSignaled()
    {
        return $this->signaled;
    }

    /**
     * Set level
     *
     * @param integer $level
     *
     * @return Comment
     */
    public function setLevel($level)
    {
        if ($level >= $this::NUM_LEVELS) {
            $this->level = $this::NUM_LEVELS;
        } elseif ($level < 0) {
            $this->level = 0;
        } else {
            $this->level = $level;
        }

        return $this;
    }

    /**
     * Get level
     *
     * @return integer
     */
    public function getLevel()
    {
        return $this->level;
    }
}

今天,我设法在控制器中发布对这些操作的评论和评论回复:

/**
 * Display form to add a NEW comment
 *
 * @Route("/articles/{slug}/comments/add", name="addComment")
 *
 */
public function addCommentAction(Article $article, Request $request)
{
    $comment = new Comment();
    $comment->setArticle($article);
    $form = $this->createForm(CommentType::class, $comment);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($comment);
        $em->flush();
        $request->getSession()->getFlashbag()->add('success', 'Le commentaire a bien été enregistré');

        return $this->redirectToRoute('view_article', array('slug' => $article->getSlug()));
    }

    return $this->render(
        'Article/addComment.html.twig',
        [
            'article' => $article,

            'form' => $form->createView(),
        ]
    );

}

/**
 * Display form to add a NEW comment
 *
 * @Route("/articles/{slug}/comments/add", name="addComment")
 *
 */
public function addCommentAction(Article $article, Request $request)
{
    $comment = new Comment();
    $comment->setArticle($article);
    $form = $this->createForm(CommentType::class, $comment);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($comment);
        $em->flush();
        $request->getSession()->getFlashbag()->add('success', 'Le commentaire a bien été enregistré');

        return $this->redirectToRoute('view_article', array('slug' => $article->getSlug()));
    }

    return $this->render(
        'Article/addComment.html.twig',
        [
            'article' => $article,
            'form' => $form->createView(),
        ]
    );
}

我的Twig模板中显示树视图的最佳渲染解决方案是什么?

提前致谢! :)

0 个答案:

没有答案