你会如何修改评论系统?

时间:2011-04-26 21:56:40

标签: php database design-patterns database-design doctrine-orm

我需要实施一个带修订版的评论系统。

我正在使用Doctrine2。

我需要在编辑时存储所有注释,但只显示当前的注释,但是我需要能够显示所有旧注释并始终显示注释计数

2 个答案:

答案 0 :(得分:2)

查看DoctrineExtensions

中的Versionable

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

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

答案 1 :(得分:0)

像这样......我会让你填空:

<?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", "my@email.com", "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"

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