仅当字段发生更改时,发送电子邮件提交表单


send email on form submit only when a field has changed

我有一个表单来编辑作业,作业有一个状态列,可以是1,2或3

<?php echo $this->Form->input('status', array('label' => '', 'options' => $status)); ?>

当我提交表单时,我想检查status的值是否等于3,如果是,那么我想发送电子邮件。但是如果这个值已经是3,我不想发送这个邮件。

是否有一个简单的方法在cakephp检查前一个值的新值等?

谢谢。

不需要与会话混淆,或者确实事先设置值。

基本上,当您编辑记录时,您从表中获得当前记录的status值。如果它已经是3,我们不想发送电子邮件,所以设置一个布尔值。

按要求更新记录

如果status不为3,新状态为,则发送邮件。

我还没有填写整个方法;但是你应该明白:

$send_email = true;    
$current_status = $this->Job->field('status');
if($current_status==3) {
    $send_email = false;
}
// save the record
if($send_email==true && $this->data['Job']['status']==3) {
   //send the email
}
  1. 在保存新记录之前从数据库中读取现有记录。然后你就有东西来比较新数据了。

  2. 或者,将状态存储在会话中,并将新数据与会话进行比较。

所以当你从数据库中读取记录时,将状态保存在会话中:

$this->data = $this->Job->read(null, $id);
$this->Session->write('JobStatus.'.$this->data['Job']['id'], $this->data['Job']['status']);

当Job被编辑时,您可以根据旧值检查新值:

if (!empty($this->data)) {
  if ($this->data['Job']['status'] == 3 && $this->Session->read('JobStatus.'.$this->data['Job']['id']) != 3) {
    /**
     * Send email
     */
  }
}

你可以用源值设置一个隐藏字段,并根据提交的值检查它的值。

<?php echo $this->Form->input('old_status', array('type' => 'hidden', 'default' => $old_status)); ?>