你会如何做一个带有修改的评论系统?

发布于 2024-11-03 19:05:21 字数 105 浏览 1 评论 0原文

我需要实施一个经过修订的评论系统。

我正在使用 Doctrine2。

我需要在编辑时存储所有评论,但仅显示最后一条评论,但是我需要能够显示所有旧评论并始终显示评论数

I need to implement a comment system with revision.

I'm using Doctrine2.

I need all to store all the comments when they are edited, but only display the last for the moment, however I need to be able to show all the old comments and always show the count of comments

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2024-11-10 19:05:21

看看DoctrineExtensions中的Versionable

基本上你让你的实体实现Versionable并且添加版本字段。它与 VersionManager 捆绑在一起,允许您回滚到特定版本。

实体上的版本字段将指示修订数量。

Have a look at Versionable in the DoctrineExtensions

Basically you make your Entity implement Versionable and add a version field. It comes bundled with a VersionManager to allow you to rollback to specific versions.

The version field on the Entity will indicate the number of revisions.

哭了丶谁疼 2024-11-10 19:05:21

像这样的东西......我会让你填空:

<?php

/** @Entity */
class Comment
{

    private $name;

    private $email;

    private $website;

    /** @OneToMany(targetEntity="CommentVersion") */
    private $versions;

    public function __construct($name, $website, $email, $comment)
    {
        $this->name = $name;
        $this->email = $email;
        $this->website = $website;
        $this->setComment($comment);
    }

    public function setComment($text)
    {
        $this->versions->add(new CommentVersion($text));
    }

    public function getComment()
    {
        $latestVersion = false;
        foreach($this->versions as $version){
            if(!$latestVersion){
                $latestVersion = $version;
                continue;
            }

            if($version->getCreatedAt() > $latestVersion->getCreatedAt()){
                $latestVersion = $version;
            }
        }

        return $latestVersion->getComment();
    }

    public function getRevisionHistory()
    {
        return $this->comments->toArray();
    }

}

/** @Entity */
class CommentVersion
{

    /** @Column(type="string") */
    private $comment;

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

    public function __construct($comment)
    {
        $this->comment   = $comment;
        $this->createdAt = new \DateTime();
    }

    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    public function getComment()
    {
        return $this->comment;
    }

}

用法非常简单:

<?php

$comment = new Comment("Cobby", "http://cobbweb.me", "[email protected]", "Some comment text");
$em->persist($comment);
$em->flush();

$comment->getComment(); // Some comment text

$comment->setComment("Revision 2");
$dm->flush();

$comment->getComment(); // Revision 2

$comment->getRevisionHistory();
// CommentVersion(0): "Some comment text"
// CommentVersion(1): "Revision 2"

我还没有测试过这个,但你明白了......

Something like this... I'll let you fill in the blanks:

<?php

/** @Entity */
class Comment
{

    private $name;

    private $email;

    private $website;

    /** @OneToMany(targetEntity="CommentVersion") */
    private $versions;

    public function __construct($name, $website, $email, $comment)
    {
        $this->name = $name;
        $this->email = $email;
        $this->website = $website;
        $this->setComment($comment);
    }

    public function setComment($text)
    {
        $this->versions->add(new CommentVersion($text));
    }

    public function getComment()
    {
        $latestVersion = false;
        foreach($this->versions as $version){
            if(!$latestVersion){
                $latestVersion = $version;
                continue;
            }

            if($version->getCreatedAt() > $latestVersion->getCreatedAt()){
                $latestVersion = $version;
            }
        }

        return $latestVersion->getComment();
    }

    public function getRevisionHistory()
    {
        return $this->comments->toArray();
    }

}

/** @Entity */
class CommentVersion
{

    /** @Column(type="string") */
    private $comment;

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

    public function __construct($comment)
    {
        $this->comment   = $comment;
        $this->createdAt = new \DateTime();
    }

    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    public function getComment()
    {
        return $this->comment;
    }

}

Usage is pretty simple:

<?php

$comment = new Comment("Cobby", "http://cobbweb.me", "[email protected]", "Some comment text");
$em->persist($comment);
$em->flush();

$comment->getComment(); // Some comment text

$comment->setComment("Revision 2");
$dm->flush();

$comment->getComment(); // Revision 2

$comment->getRevisionHistory();
// CommentVersion(0): "Some comment text"
// CommentVersion(1): "Revision 2"

I haven't tested this, but you get the idea...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文