Php 保存实体条令/symfony2的副本
我想保留实体的早期版本。当“旧”实体更新时,我想用相同的id保存它,但版本号不同,所以看起来像这样 id:1版本号 id:1修订号:2 这就是实体Php 保存实体条令/symfony2的副本,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我想保留实体的早期版本。当“旧”实体更新时,我想用相同的id保存它,但版本号不同,所以看起来像这样 id:1版本号 id:1修订号:2 这就是实体 namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * Form * * @ORM\Table() * @ORM\Entity * @ORM\HasLifecy
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();
}
}
有没有办法做到我所描述的 我想你需要的是。检查此链接: 可记录行为跟踪记录更改,并能够管理版本
使用可记录行为,您可以使用
revert()
方法从存储库还原版本。你可以在我提供给你的网站上找到许多例子。如果你不想为此使用第三方捆绑包:
您的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();
// .....
}
或者,您可以根据您的
ID
+修订版
创建一个复合主键。请记住在以后的DB调用中过滤掉任何修订版,因为一个简单的findAll
将以任何状态返回您的实体。我现在就来研究一下。我会让你知道结果的。谢谢你的建议!使用了@囚犯的建议,效果非常好。谢谢!