<?xml version="1.0"?>
<record><wiki id="wiki?page_id=39">
	<page_name>Drag_and_Drop_Reordering_of_Relationships</page_name>
	<page_id>39</page_id>
	<page_title>Drag_and_Drop_Reordering_of_Relationships</page_title>
	<content>==Drag and Drop Reordering of Related Records in Xataface==

One powerful aspect of Xataface is its abstraction of relationships between tables.  Once you define a relationship users can browse related records, add new and existing related records, and delete related records to or from the relationship.  For example, suppose you have a &apos;&apos;people&apos;&apos; table and a &apos;&apos;publications&apos;&apos;, you could define a relationship from &apos;&apos;people&apos;&apos; to &apos;&apos;publications&apos;&apos; to identify publications that belong to a particular person.  The table&apos;s might look something like:

===people table===

{| class=&quot;listing listing2&quot;
! person_id
! person_name
|-
| 1
| John Smith
|-
| 2
| Ben Stein
|-
| 3
| Harley Davidson
|-
| 4
| Trevor Linden
|}


===publications table===

{| class=&quot;listing listing2&quot;
! publication_id
! owner_id
! pub_title
|-
| 1
| 2
| Introduction to widgetry
|-
| 2
| 2
| Intermediate Widgetry
|-
| 3
| 2
| Advanced Widgetry
|-
| 4
| 3
| How to play the violin
|}


See http://xataface.com/documentation/tutorial/getting_started/relationships for an introduction to relationships in Xataface, and how to define them.

Our relationship definition in the  &apos;&apos;people&apos;&apos; table&apos;s [[relationships.ini file]] might look something like:

&lt;code&gt;
[my_pubications]
    publications.owner_id=&quot;$person_id&quot;
&lt;/code&gt;

This indicates that any publication whose &apos;&apos;owner_id&apos;&apos; column matches the current &apos;&apos;people&apos;&apos; record&apos;s &apos;&apos;person_id&apos;&apos; field is considered part of the relationship.  Using our example data above, this would mean that Ben Stein&apos;s &apos;&apos;my_publications&apos;&apos; relationship would contain 3 records: &quot;Introduction to Widgetry&quot;, &quot;Intermediate Widgetry&quot;, and &quot;Advanced Widgetry&quot;.  In our Xataface application this means that in the &quot;Details&quot; tab for the &quot;Ben Stein&quot; record, we would see a sub-tab &apos;&apos;My Publications&apos;&apos; which would contain a list of Ben Stein&apos;s publications.

===Adding Publication Order===

Now that we have our relationship, what if Ben Stein wants his publications to appear in a particular order whenever they are displayed to the user.  Xataface allows us to specify an &quot;order&quot; column for our relationship, which will cause the related records to be orderable by drag and drop (if the user has appropriate permissions).

# Add a field to the &apos;&apos;publications&apos;&apos; table named &quot;pub_order&quot;
# Change the relationship definition in the people table&apos;s [[relationships.ini file]] to tell Xataface to use the pub_order field for sorting:&lt;code&gt;
[my_pubications]
    publications.owner_id=&quot;$person_id&quot;
    metafields:order=&quot;pub_order&quot;
&lt;/code&gt;

Now load up your application and navigate to the &quot;My Publications&quot; tab for the &quot;Ben Stein&quot; record. Now you&apos;re able to drag and drop rows in your application to reorder them.

===Permission to Reorder Records===

Only users who have been assigned the &apos;&apos;reorder_related_records&apos;&apos; permission for a record&apos;s relationship are allowed to reorder records of that relationship.  By default the &apos;&apos;EDIT&apos;&apos; role contains this permission, as does any role that permits the user to edit the parent record of the relationship.  The &apos;&apos;READ ONLY&apos;&apos; role does not contain this permission.  If you want to explicitly deny this permission for all relationships in a record, you can simply set this permission to 0 in the associated role within your application&apos;s [[permissions.ini file]]:

&lt;code&gt;
[MY ROLE]
     reorder_related_records=0
&lt;/code&gt;

For more information about permssions see:

* [[permissions.ini file|The Permissions.ini file reference]]
* [http://xataface.com/documentation/tutorial/getting_started/permissions Introduction to Permissions]

===Loading Related Records using the Xataface API===

The ordering that is set via this drag and drop method is also honoured by the Xataface API when fetching related records.  You can load related records using the [http://dataface.weblite.ca/getRelatedRecordObjects getRelatedRecordObjects] method of the [http://dataface.weblite.ca/Dataface_Record Dataface_Record] class. E.g.

&lt;code&gt;
$benStein = df_get_record(&apos;people&apos;, array(&apos;person_id&apos;=&gt;2));
$pubs = $benStein-&gt;getRelatedRecordObjects(&apos;my_publications&apos;);
foreach ($pubs as $pub){
    echo $pub-&gt;val(&apos;pub_title&apos;).&apos;&lt;br/&gt;&apos;;
}
&lt;/code&gt;
This will list up to the first 30 publications of Ben stein in the order prescribed by our drag and drop ordering scheme.
</content>
	<keywords></keywords>
	<language>en</language>
	<original_page>0</original_page>
</wiki></record>