Symfony软删除

在应用中有的时候难免要进行一些删除记录操作,但是这个时候如果有外键关联,并且不想把关联数据删除掉就很麻烦。

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

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

步骤如下:

1:通过composer要安装stof这个bundle

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

 2:在app kernel中注册这个bundle

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

 3:在config文件中增加配置

  在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:修改需要软删除的entity

增加引用
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;
    }

 

转载于:https://www.cnblogs.com/wlemory/p/5224482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值