如何判断我是在编辑还是创建之前保存?CakePHP [英] How can I tell if I'm in beforeSave from an edit or a create? CakePHP

查看:40
本文介绍了如何判断我是在编辑还是创建之前保存?CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,我需要在保存之前进行一些处理(或在某些情况下进行编辑),但在简单编辑时通常不需要.事实上,如果我对大多数编辑进行处理,结果字段将是错误的.现在,我正在处理模型的 beforeSave 回调.如何判断我是来自编辑还是添加?

I have a model where I need to do some processing before saving (or in certain cases with an edit) but not usually when simply editing. In fact, if I do the processing on most edits, the resulting field will be wrong. Right now, I am working in the beforeSave callback of the model. How can I tell if I came from the edit or add?

弗兰克·卢克

推荐答案

function beforeSave() {
  if (!$this->id && !isset($this->data[$this->alias][$this->primaryKey])) {
    // insert
  } else {
    // edit
  }
  return true;
}

这篇关于如何判断我是在编辑还是创建之前保存?CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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