访问 Symfony 博客中的嵌套评论

Accessing to nested comments in a Symfony Blog

我正在开发一个实现 public 提要的项目。我有一个 Comment 实体,让用户可以向每个提要提交评论。但是,我希望用户也能够提交对评论的回复。

为此,我的 Comment 实体中有 $response 属性。我假设保存回复与保存评论相同,因为回复也是评论。

但是,我不确定如何在没有 id 的情况下访问评论并保存对该评论的回复。

在我的 FeedController 我保存这样的评论;

 $commentForm = $this->createForm(CommentType::class);
    $commentForm->handleRequest($request);
    if ($commentForm->isSubmitted() && $commentForm->isValid()) {
        $content = $commentForm->get('content')->getData();
        $feed_id = $feed;

        $comment= EntityBuilder::newComment($content, $user, $feed_id);

        $commentService->saveEntity($comment);

        if(!is_null($comment)){
            $this->addFlash(
                'commentsuccess',
                'Your reply was successfully posted!'
            );
        }

        return $this->redirect($this->generateUrl('showFeed', array('slug' => $feed->getSlug()), UrlGeneratorInterface::ABSOLUTE_URL));

    }

这里是评论实体;

/**
* Class Comment
* @package AppBundle\Entity
* @ORM\Entity(repositoryClass="AppBundle\Repository\CommentRepository")
* @ORM\Table(name="comment")
* @ORM\HasLifecycleCallbacks()
*
*/
class Comment
{

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

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Feed", inversedBy="comments")
 * @ORM\JoinColumn(name="feedid", referencedColumnName="id")
 */
private $feedid;

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="comments")
 * @ORM\JoinColumn(name="createdby", referencedColumnName="id")
 */
private $createdby;

/**
 * @ORM\Column(type="string")
 * @Assert\NotBlank(message="Please fill in the description")
 */
private $content;

/**
 * @ORM\OneToOne(targetEntity="Comment")
 */
protected $response;


/**
 * @ORM\Column(type="datetime")
 */
private $createdAt;

/**
 * @ORM\Column(type="datetime")
 */
private $updatedAt;


/**
 * Constructor
 */
public function __construct()
{
    $this->createdAt= new \DateTime();
    $this->updatedAt= new \DateTime();
}

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

/**
 * 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 feedid
 *
 * @param \AppBundle\Entity\Feed $feedid
 *
 * @return Comment
 */
public function setFeedid(\AppBundle\Entity\Feed $feedid = null)
{
    $this->feedid = $feedid;

    return $this;
}

/**
 * Get feedid
 *
 * @return \AppBundle\Entity\Feed
 */
public function getFeedid()
{
    return $this->feedid;
}

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

    return $this;
}

/**
 * Get createdby
 *
 * @return \AppBundle\Entity\User
 */
public function getCreatedby()
{
    return $this->createdby;
}

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

    return $this;
}

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

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

    return $this;
}

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


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

    return $this;
}

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

}

即使我为回复创建了另一种表单类型,我仍然不确定如何保存对特定评论的回复。

我的建议是实施 Parent/Children 方法,将 (ManyToOne) $parent 字段添加到 Comment 实体及其反转 (OneToMany)$children属性。稍后,您可以为 $children 属性 添加一个 CollectionType 表单字段到 CommentType 中,因此您可能需要对每个评论进行多个回复。

不用担心父 idCollectionType 会为您完成工作。 详细了解 CollectionType 的工作原理 here