2011-04-26 54 views
2

我需要实施带修订的评论系统。你会如何做一个评论系统修订?

我正在使用Doctrine2。

我需要的所有存储所有的意见时,他们进行编辑,但只显示最后的时刻,但是我需要能够显示所有旧的意见,并始终显示的评论

回答

2

拥有计数看看版本可控在DoctrineExtensions

基本上,你让你的实体实施Versionable并添加一个版本字段。它捆绑了一个VersionManager以允许您回滚到特定版本。

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

+0

很有意思,我来看看 – JohnT 2011-04-26 23:09:11

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", "[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" 

我没有测试过这一点,但你的想法...