Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 您将如何使用修订版的评论系统?_Php_Database_Design Patterns_Database Design_Doctrine Orm - Fatal编程技术网

Php 您将如何使用修订版的评论系统?

Php 您将如何使用修订版的评论系统?,php,database,design-patterns,database-design,doctrine-orm,Php,Database,Design Patterns,Database Design,Doctrine Orm,我需要实施一个带有修订的评论系统 我正在使用Doctrine2 我需要全部来存储编辑时的所有评论,但目前仅显示最后一条评论,但是我需要能够显示所有旧评论并始终显示评论计数 基本上,您可以使实体实现可版本化,并添加一个版本字段。它与VersionManager捆绑在一起,允许您回滚到特定版本 实体上的版本字段将指示修订的数量。类似这样的内容。。。我让你来填空: <?php /** @Entity */ class Comment { private $name; pri

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

我正在使用Doctrine2


我需要全部来存储编辑时的所有评论,但目前仅显示最后一条评论,但是我需要能够显示所有旧评论并始终显示评论计数

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


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

类似这样的内容。。。我让你来填空:

<?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"