Current Record: beforeSave #109

Back to Delegate class methods Table of Contents Synopsis Method Signature Parameters Returns Examples See Also Synopsis The bef...

Current Record: beforeSave #109

Back to Delegate class methods Table of Contents Synopsis Method Signature Parameters Returns Examples See Also Synopsis The bef...

beforeSave Trigger

[Permalink]

Back to Delegate class methods

Synopsis

The beforeSave trigger can be implemented in any table's Delegate Class Methods? to perform functionality that should be run *before* a record of that table is saved. This is a useful place to insert additional field values depending on the input of the save record form.

This method is called both when records are inserted and when existing records are updated. Some other triggers include beforeInsert?, beforeUpdate?, afterSave?, afterInsert?, and afterUpdate?, which do what you might expect.

Method Signature

function beforeSave( Dataface_Record $record);

Parameters

  1. $record - The record that is about to be saved. This is a Dataface_Record object.

Returns

  1. Dataface_Error on failure (if you want to cancel the save).

Examples

Given a table named "people", suppose we wanted to automatically populate a field named "full_name" with the concatenation of the "first_name" and "last_name" fields. (Note you could also achieve a similar thing by making a calculated field for "full_name", but for this example, we assume that we actually want to store this in the database.

We create a beforeSave() trigger that automatically updates the "full_name" field every time the record is saved.

In the "people" table delegate class (i.e. tables/people/people.php)

class tables_people {
    function beforeSave($record){
        $record->setValue('full_name', 
            $record->val('first_name').' '.$record->val('last_name')
        );
    }
}

See Also

blog comments powered by Disqus
Powered by Xataface
(c) 2005-2024 All rights reserved