2011-04-26 59 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" 

我沒有測試過這一點,但你的想法...