beforeSave Trigger
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
- $record - The record that is about to be saved. This is a Dataface_Record object.
Returns
- 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
var disqus_identifier = ‘xataface.com/wiki/beforeSave’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘http://xataface.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })(); blog comments powered by Disqus
//<![CDATA[ (function() { var links = document.getElementsByTagName(‘a’); var query = ‘?’; for(var i = 0; i < links.length; i++) { if(links[i].href.indexOf(‘#disqus_thread’) >= 0) { query += ‘url’ + i + ‘=’ + encodeURIComponent(links[i].href) + ‘&’; } } document.write(‘