In CakePHP, how can you determine if a field was changed in an edit action?

With reference to Alexander Morland Answer.

How about this instead of looping through it in before filter.

$result = array_diff_assoc($this->old[$this->alias],$this->data[$this->alias]);

You will get key as well as value also.


To monitor changes in a field, you can use this logic in your model with no changes elsewhere required:

function beforeSave() {
    $this->recursive = -1;
    $this->old = $this->find(array($this->primaryKey => $this->id));
    if ($this->old){
        $changed_fields = array();
        foreach ($this->data[$this->alias] as $key =>$value) {
            if ($this->old[$this->alias][$key] != $value) {
                $changed_fields[] = $key;
            }
        }
    }
    // $changed_fields is an array of fields that changed
    return true;
}

Tags:

Php

Cakephp