<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.7.2" -->
<rss version="2.0">
    <channel>
        <title>beforeAddRelatedRecord[ Search for &quot;&quot;]</title>
        <description></description>
        <link>http://xataface.com/wiki/index.php?-action=single_record_search&amp;-table=wiki&amp;page_id=%3D108&amp;-cursor=0&amp;-skip=0&amp;-limit=30&amp;-mode=list&amp;--subsearch=</link>
        <lastBuildDate>Tue, 21 May 2013 22:20:57 +0100</lastBuildDate>
        <generator>FeedCreator 1.7.2</generator>
        <item>
            <title>beforeAddRelatedRecord</title>
            <link>http://www.xataface.com/wiki/beforeAddRelatedRecord</link>
            <description>&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Field&lt;/th&gt;&lt;th&gt;Value&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;Page name&lt;/td&gt;&lt;td valign=&quot;top&quot;&gt;beforeAddRelatedRecord&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;Page id&lt;/td&gt;&lt;td valign=&quot;top&quot;&gt;108&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;Page title&lt;/td&gt;&lt;td valign=&quot;top&quot;&gt;beforeAddRelatedRecord Delegate Class Method&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;Content&lt;/td&gt;&lt;td valign=&quot;top&quot;&gt;&lt;p&gt;Return to &lt;a href=&quot;/wiki/Delegate_class_methods&quot;&gt;Delegate class methods&lt;/a&gt;&lt;/p&gt;

&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt;&lt;tr&gt;&lt;td&gt;
&lt;div id=&quot;toc&quot;&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;
	&lt;div style=&quot;margin-left: 0em;&quot;&gt;&lt;a href=&quot;#toc0&quot;&gt;Synopsis&lt;/a&gt;&lt;/div&gt;
	&lt;div style=&quot;margin-left: 0em;&quot;&gt;&lt;a href=&quot;#toc1&quot;&gt;Method Signature&lt;/a&gt;&lt;/div&gt;
	&lt;div style=&quot;margin-left: 0em;&quot;&gt;&lt;a href=&quot;#toc2&quot;&gt;Parameters&lt;/a&gt;&lt;/div&gt;
	&lt;div style=&quot;margin-left: 1em;&quot;&gt;&lt;a href=&quot;#toc3&quot;&gt;Returns&lt;/a&gt;&lt;/div&gt;
	&lt;div style=&quot;margin-left: 0em;&quot;&gt;&lt;a href=&quot;#toc4&quot;&gt;Examples&lt;/a&gt;&lt;/div&gt;
	&lt;div style=&quot;margin-left: 1em;&quot;&gt;&lt;a href=&quot;#toc5&quot;&gt;Example 1: Permission Check&lt;/a&gt;&lt;/div&gt;
	&lt;div style=&quot;margin-left: 0em;&quot;&gt;&lt;a href=&quot;#toc6&quot;&gt;See Also&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;


&lt;h2 id=&quot;toc0&quot;&gt;Synopsis&lt;/h2&gt;
&lt;p&gt;The &lt;em&gt;beforeAddRelatedRecord&lt;/em&gt; delegate class method can be implemented in any table's delegate class.  It will be executed before any related record is added to that table'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.&lt;/p&gt;


&lt;h2 id=&quot;toc1&quot;&gt;Method Signature&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;function beforeAddRelatedRecord( Dataface_RelatedRecord $record );&lt;/code&gt;&lt;/pre&gt;


&lt;h2 id=&quot;toc2&quot;&gt;Parameters&lt;/h2&gt;
&lt;ul&gt;
    &lt;li&gt; &lt;strong&gt;$record&lt;/strong&gt; - The &lt;a href=&quot;http://dataface.weblite.ca/Dataface_RelatedRecord&quot; onclick=&quot;window.open(this.href, '_blank'); return false;&quot;&gt;Dataface_RelatedRecord&lt;/a&gt; object encapsulating the record that is being added to the relationship.&lt;/li&gt;
&lt;/ul&gt;


&lt;h3 id=&quot;toc3&quot;&gt;Returns&lt;/h3&gt;
&lt;ul&gt;
    &lt;li&gt; void - If all is well&lt;/li&gt;
    &lt;li&gt; &lt;a href=&quot;http://dataface.weblite.ca/Dataface_Error&quot; onclick=&quot;window.open(this.href, '_blank'); return false;&quot;&gt;Dataface_Error&lt;/a&gt; object if something went wrong.  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is quite common to return a permission denied error from this method after doing some checks. e.g.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;function beforeAddRelatedRecord($record){
    if ( /* some checks here */ ){
        return Dataface_Error::permissionDenied('Sorry you don\'t have permission to do this.');
    }
}&lt;/code&gt;&lt;/pre&gt;


&lt;h2 id=&quot;toc4&quot;&gt;Examples&lt;/h2&gt;

&lt;h3 id=&quot;toc5&quot;&gt;Example 1: Permission Check&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;function beforeAddRelatedRecord($record){
    if ( $record-&amp;gt;_relationshipName == 'program_roles' ){
        // check to make sure that we are allowed to add roles to this program
        $program = df_get_record('programs', array('program_id'=&amp;gt;'='.$record-&amp;gt;val('program_id')));
        if ( !$program ){
            return Dataface_Error::permissionDenied(&amp;quot;Program could not be found&amp;quot;);
        }
        if ( !$program-&amp;gt;checkPermission('add new related record', array(
                'relationship' =&amp;gt; 'program_roles'
                )
             )
           ){
            return Dataface_Error::permissionDenied(&amp;quot;You don't have permission to add roles to this program&amp;quot;);
        }
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above example requires a little bit of context to understand.  This method is defined in the 'users' table.  There is a relationship between the &lt;em&gt;users&lt;/em&gt; table and the &lt;em&gt;programs&lt;/em&gt; table called 'program_roles'.  It keeps track of the roles that users have with respect to programs.  There is a mirror relationshp in the &lt;em&gt;programs&lt;/em&gt; table that goes to the &lt;em&gt;users&lt;/em&gt; table, also named &lt;em&gt;program_roles&lt;/em&gt;.  Both the &lt;em&gt;users&lt;/em&gt; table and the &lt;em&gt;programs&lt;/em&gt; table contain rel_program_roles__roles() methods to define who can and cannot add to this relationship.  However these don'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).&lt;/p&gt;

&lt;p&gt;So, in the &lt;em&gt;users&lt;/em&gt; table we defined the &lt;em&gt;beforeAddRelatedRecord&lt;/em&gt; above which checks the permissions of the programs table to see if that particular program can be added to this relationship by this user.&lt;/p&gt;


&lt;h2 id=&quot;toc6&quot;&gt;See Also&lt;/h2&gt;
&lt;ul&gt;
    &lt;li&gt; &lt;a href=&quot;http://dataface.weblite.ca/Dataface_RelatedRecord&quot; onclick=&quot;window.open(this.href, '_blank'); return false;&quot;&gt;Dataface_RelatedRecord API Docs&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt; &lt;a href=&quot;/wiki/Delegate_class_methods&quot;&gt;Delegate class methods&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt; &lt;a href=&quot;http://xataface.com/documentation/tutorial/getting_started/relationships&quot; onclick=&quot;window.open(this.href, '_blank'); return false;&quot;&gt;Introduction to Relationships&lt;/a&gt; - from the Getting Started Tutorial&lt;/li&gt;
    &lt;li&gt; &lt;a href=&quot;/wiki/relationships.ini_file&quot;&gt;relationships.ini file&lt;/a&gt; reference&lt;/li&gt;
&lt;/ul&gt;

&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;Keywords&lt;/td&gt;&lt;td valign=&quot;top&quot;&gt;beforeAddRelatedRecord, Delegate class methods, relationship triggers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;Language&lt;/td&gt;&lt;td valign=&quot;top&quot;&gt;en&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description>
            <author>beforeAddRelatedRecord</author>
        </item>
    </channel>
</rss>
