Make a difference between insertion and update

Published on and tagged with cakephp  tip

If you want to make a difference between insertion and update in your model, e.g. in your beforeSave() function, you can test if the id of the model is not null:

function beforeSave()
{
    if ($this->id == null)
    {
        // it is an insert, do something
    }
    else
    {
        // it is an update, do something
    }			
    return true;
}

Thanks to gwoo for the hint, and to Frederic Logier (aka fredix) for his questions.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License