douqiaolong0528 2014-11-22 07:34
浏览 33
已采纳

Cakephp删除全部后

Why does cakephp have not any callback for after saveAll and deleteAll like beforeSave() and beforeDelete()? I know cakephp uses foreach for deleteAll() and saveAll which uses default delete() and save() function. But doesn't it uses too much queries, if i'm set any function like afterSave(), beforeSave() as it will execute every time whenever save() function will be execute.

Doesn't cakephp require separate function for saveAll(), deleteAll()? Correct me if i'm wrong.

  • 写回答

1条回答 默认 最新

  • doukuang1950 2014-11-22 08:46
    关注

    As you already said yourself, saveAll and deleteAll are basically just wrappers around save() and delete(). Therefor it will trigger beforeSave() and beforeDelete() for each row of data is saves or deletes. So adding a separate beforeSaveAll and beforeDeleteAll isn't really necessary. Yes, it could run a lot of queries, but that doesn't have to be a problem. It does offer a way to mould every row of data that is saved or deleted rather than doing one single bulk operation that can either work or fail entirely (because they're all dependent of each other when you bundle them into one operation).

    If you would want to add any custom logic to every saveAll and deleteAll action (be very sure that is what you actually want! And remember that the regular beforeSave and beforeDelete will still be called unless you disable the callbacks in each saveAll and deleteAll), you can simply overwrite the methods with your own implentation in your AppModel and have it call your custom "before" logic, like:

    // Add your own custom deleteAll and saveAll to AppModel
    public function deleteAll($conditions, $cascade = true, $callbacks = false) {
        if ($this->beforeDeleteAll($conditions)) {
            parent::deleteAll($conditions, $cascade, $callbacks);
        } else {
            return false;
        }
    }
    
    public function saveAll($data = array(), $options = array()) {
        if ($this->beforeSaveAll($data, $options)) {
            parent::saveAll($data, $options);
        } else {
            return false;
        }
    }
    
    // As well as your custom logic
    public function beforeDeleteAll($conditions) {
        // Do your stuff here
        return true;
    }
    
    public function beforeSaveAll($data, $options) {
        // Do your stuff here
        return true;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景