Tuesday, December 16, 2008

Getting related records

One thing you'll want to do is to grab related records in Xataface. Say for example, you have an abstract record that belongs to a conference. In the relationships.ini file of your abstract table, you would include something like this:


[Conference]
conference.ConferenceID="$AbstractConferenceID"


Where the conference is the name of the conference table, ConferenceID is the name of the index of that table, and $AbstractConferenceID is the foreign ID in your abstract record that corresponds to the the particular record in the conference table. Note the name of the relationship is "Conference."

In Xataface, if you have an abstract record you can quickly retrieve the related conference by simply doing this:


$conference = $record->getRelatedRecord('Conference')


This will retrieve the conference as a xataface_related_record object. It is important to realize that this is not the same thing as a xataface_record. If you want to convert this related_record object into a xataface_record object you can use the toRecord() function:

$conference = $record->getRelatedRecord('Conference')->toRecord();


This is a powerful technique that can save a lot of time.

No comments: