Я хочу сохранить предыдущую версию объекта. Когда «старый» объект обновляется, я хочу, чтобы сохранить его с тем же идентификатором, но с другим номером версии, так это выглядит примерно такСохраните копию доктрины объекта/symfony2
ид: 1 revision_number: 1
ид : 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();
}
}
Есть ли способ сделать то, что я описал?
или вы могли бы просто создать составной первичный ключ, основанный с вашего '' ID' + Revision'. – Prisoner
yep, или это :) – DevDonkey
Не забудьте отфильтровать любые изменения в ваших вызовах БД после этого, поскольку простой 'findAll' вернет ваши объекты в любом состоянии. – ferdynator