保存实体doctrine / symfony2的副本

时间:2015-10-12 12:14:10

标签: php symfony doctrine-orm

我想保留以前版本的实体。当'旧'实体更新时,我想用相同的id保存它,但是使用不同的版本号,所以它看起来像这样

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 个答案:

答案 0 :(得分:4)

我认为您可能需要的是Loggable extension for Doctrine。检查此链接:

https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/loggable.md

  

可记录行为会跟踪您的记录更改,并且能够管理版本。

使用loggable行为,您可以使用revert()方法从存储库中恢复版本。你可以在网站上找到很多我给你链接的例子。

答案 1 :(得分: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();

    // .....


}