<?xml version="1.0"?>
<record><wiki id="wiki?page_id=108">
	<page_name>beforeAddRelatedRecord</page_name>
	<page_id>108</page_id>
	<page_title>beforeAddRelatedRecord Delegate Class Method</page_title>
	<content>Return to [[Delegate class methods]]

[[toc]]

==Synopsis==

The &apos;&apos;beforeAddRelatedRecord&apos;&apos; delegate class method can be implemented in any table&apos;s delegate class.  It will be executed before any related record is added to that table&apos;s relationships.  Since it will be fired for all relationships on the table, you will have to include logic to only execute if the relationship that you are targeting is being added to.

==Method Signature==

&lt;code&gt;
function beforeAddRelatedRecord( Dataface_RelatedRecord $record );
&lt;/code&gt;

==Parameters==

* &apos;&apos;&apos;$record&apos;&apos;&apos; - The [http://dataface.weblite.ca/Dataface_RelatedRecord Dataface_RelatedRecord] object encapsulating the record that is being added to the relationship.

===Returns===

* void - If all is well
* [http://dataface.weblite.ca/Dataface_Error Dataface_Error] object if something went wrong.  

It is quite common to return a permission denied error from this method after doing some checks. e.g.

&lt;code&gt;
function beforeAddRelatedRecord($record){
    if ( /* some checks here */ ){
        return Dataface_Error::permissionDenied(&apos;Sorry you don\&apos;t have permission to do this.&apos;);
    }
}
&lt;/code&gt;

==Examples==

===Example 1: Permission Check===

&lt;code&gt;
function beforeAddRelatedRecord($record){
    if ( $record-&gt;_relationshipName == &apos;program_roles&apos; ){
        // check to make sure that we are allowed to add roles to this program
        $program = df_get_record(&apos;programs&apos;, array(&apos;program_id&apos;=&gt;&apos;=&apos;.$record-&gt;val(&apos;program_id&apos;)));
        if ( !$program ){
            return Dataface_Error::permissionDenied(&quot;Program could not be found&quot;);
        }
        if ( !$program-&gt;checkPermission(&apos;add new related record&apos;, array(
                &apos;relationship&apos; =&gt; &apos;program_roles&apos;
                )
             )
           ){
            return Dataface_Error::permissionDenied(&quot;You don&apos;t have permission to add roles to this program&quot;);
        }
    }
}
&lt;/code&gt;

The above example requires a little bit of context to understand.  This method is defined in the &apos;users&apos; table.  There is a relationship between the &apos;&apos;users&apos;&apos; table and the &apos;&apos;programs&apos;&apos; table called &apos;program_roles&apos;.  It keeps track of the roles that users have with respect to programs.  There is a mirror relationshp in the &apos;&apos;programs&apos;&apos; table that goes to the &apos;&apos;users&apos;&apos; table, also named &apos;&apos;program_roles&apos;&apos;.  Both the &apos;&apos;users&apos;&apos; table and the &apos;&apos;programs&apos;&apos; table contain rel_program_roles__roles() methods to define who can and cannot add to this relationship.  However these don&apos;t discern on the content that is being added to the relationship, so it is still possible that an ineligible program could be added to the relationship via the users table (or vice versa).

So, in the &apos;&apos;users&apos;&apos; table we defined the &apos;&apos;beforeAddRelatedRecord&apos;&apos; above which checks the permissions of the programs table to see if that particular program can be added to this relationship by this user.


==See Also==

* [http://dataface.weblite.ca/Dataface_RelatedRecord Dataface_RelatedRecord API Docs]
* [[Delegate class methods]]
* [http://xataface.com/documentation/tutorial/getting_started/relationships Introduction to Relationships] - from the Getting Started Tutorial
* [[relationships.ini file]] reference
</content>
	<keywords>beforeAddRelatedRecord, Delegate class methods, relationship triggers</keywords>
	<language>en</language>
	<original_page></original_page>
</wiki></record>