douju9272 2010-11-22 11:58
浏览 50
已采纳

防止Doctrine记录被删除的正确方法

Can I just override delete() method of Doctrine_Record?

class myRec extends myRecBase
  public function delete() {
    return false;
  }
}

Or use preDelete hook? Or throw an exception in delete()?

Any help will be appreciated.

  • 写回答

2条回答 默认 最新

  • dongwei2610 2010-11-22 18:31
    关注

    One option is to use the preDelete method to simply skip the delete method all together.

    Example:

    public function preDelete(Doctrine_Event $event)
    {
       $event->skipOperation();
    }
    

    You should be able to do this within your extended Doctrine_Record classes or, as an alternative, with a listener. A listener may be beneficial in the cases where you want some part of your application to have the ability to delete records (e.g. an admin section vs client side code).

    With the listener approach you create a listener with the above preDelete method and register it with your Table like this:

    $userTable = Doctrine_Core::getTable('User');
    $userTable->addRecordListener(new HydrationListener());
    

    If the listener is not registered you will still be able to delete records. If the listener is registered your deletes will not persist to the database.

    More information about that method can be found here: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/event-listeners/en

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗