Symfony软删除

在应用中有的时候不免要进行一些删除记录操做,可是这个时候若是有外键关联,而且不想把关联数据删除掉就很麻烦。php

解决方法是能够采用symfony自带的软删除功能html

官方文档在:https://symfony.com/doc/master/bundles/StofDoctrineExtensionsBundle/index.html#activate-the-extensions-you-wantapp

步骤以下:composer

1:经过composer要安装stof这个bundleide

   "require": {
        "stof/doctrine-extensions-bundle": "dev-master",
         ...

 2:在app kernel中注册这个bundleui

    public function registerBundles()
    {
        $bundles = array(
            new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
     }

 3:在config文件中增长配置this

  在doctrine的orm下增长filters部分  
orm: auto_generate_proxy_classes: "%kernel.debug%" naming_strategy: doctrine.orm.naming_strategy.underscore auto_mapping: true filters: soft-deleteable: class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter # Required enabled: true

 

    stof_doctrine_extensions:
default_locale: en_US orm:
default: tree: true
timestampable: true # not needed: listeners are not enabled by default softdeleteable: true

4:修改须要软删除的entityspa

增长引用
use Gedmo\Mapping\Annotation as Gedmo;

增长标记字段
 /**
  * Video
  *
  * @ORM\Table(name="videos")
  * @ORM\Entity(repositoryClass="Tron\WebBundle\Repository\VideoRepository")
  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)//添加此行
  */
添加字段deleteat
    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $deletedAt;

    /**
     * Get isDelete
     *
     * @return boolean
     */
    public function getIsDelete()
    {
        return $this->isDelete;
    }

    public function getDeletedAt()
    {
        return $this->deletedAt;
    }
相关文章
相关标签/搜索