微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 保存实体doctrine / symfony2的副本

我想保留以前版本的实体.当’旧’实体更新时,我想用相同的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();
    }

}

有没有办法可以做我描述的事情?

解决方法:

我想你可能需要的是Loggable extension for Doctrine.检查这个链接

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

Loggable behavior tracks your record changes and is able to manage versions.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐