在 Symfony/Doctrine 中删除记录时执行一些清理 [英] Perform some cleanup when deleting a record in Symfony/Doctrine

查看:22
本文介绍了在 Symfony/Doctrine 中删除记录时执行一些清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Doctrine 中使用 Symfony 1.4.5

Using Symfony 1.4.5 with Doctrine

我有一个模型,其中包含上传的图像作为列之一 - 创建和更新记录很好(使用 doSave() 方法处理上传和对文件的任何更改).

I have a model which includes an uploaded image as one of the columns - creating and updating the record is fine (using the doSave() method to deal with the upload and any changes to the file).

我遇到的问题是如果记录被删除 - 我希望它也删除关联的文件.但是在通过文档和谷歌搜索了几个小时之后,我无论如何都找不到这样做.

The problem I'm having is if the record is deleted - I want it to remove the associated file as well. But I can't find anyway to do this after several hours of hunting through documentation and Google.

有没有办法指定某种删除后代码?

Is there a way to specify some kind of post-delete code?

推荐答案

最终解决方案:

在/lib/model/doctrine/Image.class.php

in /lib/model/doctrine/Image.class.php

class Image extends BaseImage
{
  public function postDelete()
  {
    $filename = $this->getFilename();

    $filepath = sfConfig::get('sf_upload_dir') . '/' . $filename;
    @unlink($filepath);
  }
}

感谢 Sponz 上校为我指明了正确的方向

Thanks to Colonel Sponz for pointing me in the right direction

这篇关于在 Symfony/Doctrine 中删除记录时执行一些清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆