Twitter Feed

Xataface Maillist

Sign up to receive the Xataface news letter with weekly updates and development tips.

 
Found 1 of 138 records in table Wiki
Now Showing 1 of 1

Current Record: beforeSave

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-2017 All rights reserved