<?xml version="1.0"?>
<record><wiki id="wiki?page_id=109">
	<page_name>beforeSave</page_name>
	<page_id>109</page_id>
	<page_title>beforeSave Trigger</page_title>
	<content>Back to [[Delegate class methods]]

[[toc]]



===Synopsis===

The beforeSave trigger can be implemented in any table&apos;s [[delegate class|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===

&lt;code&gt;function beforeSave( Dataface_Record $record);&lt;/code&gt;

====Parameters====

# &apos;&apos;&apos;$record&apos;&apos;&apos; - The record that is about to be saved.  This is a [http://dataface.weblite.ca/Dataface_Record Dataface_Record] object.

====Returns====

# [http://dataface.weblite.ca/Dataface_Error Dataface_Error] on failure (if you want to cancel the save).


===Examples===

Given a table named &quot;people&quot;, suppose we wanted to automatically populate a field named &quot;full_name&quot;  with the concatenation of the &quot;first_name&quot; and &quot;last_name&quot; fields.  (Note you could also achieve a similar thing by making a calculated field for &quot;full_name&quot;, 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 &quot;full_name&quot; field every time the record is saved.

In the &quot;people&quot; table delegate class (i.e. tables/people/people.php)
&lt;code&gt;
class tables_people {
    function beforeSave($record){
        $record-&gt;setValue(&apos;full_name&apos;, 
            $record-&gt;val(&apos;first_name&apos;).&apos; &apos;.$record-&gt;val(&apos;last_name&apos;)
        );
    }
}
&lt;/code&gt;

==See Also==

* [http://www.xataface.com/documentation/tutorial/getting_started/triggers Triggers section of the Xataface Getting Started Tutorial]
</content>
	<keywords>triggers, beforeSave,</keywords>
	<language>en</language>
	<original_page></original_page>
</wiki></record>