2015-10-12 50 views
3

我想保留實體的以前版本。當「老」實體更新我想它使用相同的ID,但具有不同的版本號,所以它看起來是這樣的保存實體原則/ symfony2的副本

ID保存:1 REVISION_NUMBER:1

ID :1個REVISION_NUMBER:2

這是實體

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 


/** 
* Form 
* 
* @ORM\Table() 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks 
*/ 
class Form 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $name; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="export_template", type="string", length=255, nullable = true) 
    */ 
    private $exportTemplate; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="revision", type="datetime", nullable = true) 
    */ 
    private $revision; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="revision_number", type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $revisionNumber; 

    /** 
    * @ORM\ManyToOne(targetEntity="Client", inversedBy="forms") 
    * @ORM\JoinColumn(name="form_id", referencedColumnName="id") 
    */ 
    protected $client; 

    /** 
    * @ORM\OneToMany(targetEntity="Section", mappedBy="form", cascade={"persist"}) 
    */ 

    protected $sections; 

    /** 
    * @ORM\OneToMany(targetEntity="Inspection", mappedBy="form") 
    */ 

    protected $inspections; 

    public function __construct() 
    { 
     $this->sections = new ArrayCollection(); 
     $this->inspections = new ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return Form 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set exportTemplate 
    * 
    * @param string $exportTemplate 
    * @return Form 
    */ 
    public function setExportTemplate($exportTemplate) 
    { 
     $this->exportTemplate = $exportTemplate; 

     return $this; 
    } 

    /** 
    * Get exportTemplate 
    * 
    * @return string 
    */ 
    public function getExportTemplate() 
    { 
     return $this->exportTemplate; 
    } 

    /** 
    * Set revision 
    * 
    * @param \DateTime $revision 
    * @return Form 
    */ 
    public function setRevision($revision) 
    { 
     $this->revision = $revision; 

     return $this; 
    } 

    /** 
    * Get revision 
    * 
    * @return \DateTime 
    */ 
    public function getRevision() 
    { 
     return $this->revision; 
    } 


    /** 
    * @param $revisionNumber 
    * @return Form 
    */ 
    public function setRevisionNumber($revisionNumber) 
    { 
     $this->revisionNumber = $revisionNumber; 

     return $this; 
    } 

    /** 
    * @return int 
    */ 
    public function getRevisionNumber() 
    { 
     return $this->revisionNumber; 
    } 

    /** 
    * Set client 
    * 
    * @param \AppBundle\Entity\Client $client 
    * @return Form 
    */ 
    public function setClient(\AppBundle\Entity\Client $client = null) 
    { 
     $this->client = $client; 

     return $this; 
    } 

    /** 
    * Get client 
    * 
    * @return \AppBundle\Entity\Client 
    */ 
    public function getClient() 
    { 
     return $this->client; 
    } 

    /** 
    * Add sections 
    * 
    * @param \AppBundle\Entity\Section $sections 
    * @return Form 
    */ 
    public function addSection(\AppBundle\Entity\Section $sections) 
    { 
     $this->sections[] = $sections; 

     return $this; 
    } 

    /** 
    * Remove sections 
    * 
    * @param \AppBundle\Entity\Section $sections 
    */ 
    public function removeSection(\AppBundle\Entity\Section $sections) 
    { 
     $this->sections->removeElement($sections); 
    } 

    /** 
    * Get sections 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getSections() 
    { 
     return $this->sections; 
    } 

    /** 
    * Add inspections 
    * 
    * @param \AppBundle\Entity\Inspection $inspections 
    * @return Form 
    */ 
    public function addInspection(\AppBundle\Entity\Inspection $inspections) 
    { 
     $this->inspections[] = $inspections; 

     return $this; 
    } 

    /** 
    * Remove inspections 
    * 
    * @param \AppBundle\Entity\Inspection $inspections 
    */ 
    public function removeInspection(\AppBundle\Entity\Inspection $inspections) 
    { 
     $this->inspections->removeElement($inspections); 
    } 

    /** 
    * Get inspections 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getInspections() 
    { 
     return $this->inspections; 
    } 

    /** 
    * 
    * @ORM\PrePersist() 
    */ 

    public function preSetDate(){ 
     $this->revision = new \DateTime(); 
    } 

} 

有沒有辦法我可以做我描述的?

回答

2

如果使用第三方捆綁此不花哨:

您的ID仍然必須是唯一的。如果您需要跟蹤副本的來源,那麼您可以添加一個新參數。

一旦你決定了這一點,我會簡單地克隆它,修改版本號,並添加原始ID,如果這是你想要去的方式,然後堅持它。

class Form { 
    // .... 
    $orig_id = null; 

    // any getters/setters you need 
    // ..... 
} 

然後控制器:

public function copyEntity($entity) { 
    $new_ent = clone $entity; 

    $new_ent->setOrigId($entity->getId()); 
    $new_ent->setRevision(true); // I would probably bin this as origId being !== null would do the same job 

    $this->entityManager->persist($new_ent); 
    $this->entityManager->flush(); 

    // ..... 


} 
+2

或者你可以只創建一個基於關你'ID' +'Revision'的複合主鍵。 – Prisoner

+0

是的,或者這個:) – DevDonkey

+0

請記住在之後過濾掉DB調用中的任何修訂,因爲一個簡單的'findAll'會以任何狀態返回你的實體。 – ferdynator