Agile Tortoise

Greg Pierce’s blog

« Servoy-Twitter example: mod_twitter      Servoy: mod_js_core v0.4 »

Servoy tip: fetch n:1 record via calculation

In a Servoy solution, often you have a record and you want to access a record that is related via an n:1 relation. Say, for example, you have a relationship from contacts to companies. You are working with a contact record, and you need to fetch the related company into a variable to work with.

The proper way to do this is first to test for the presence of a record, and then fetch it through the relation you have defined. If it's something you're going to do often, you can wrap that logic in a dataprovider calculation on the contacts table, however. Try defining a "MEDIA" type calculation something like this:

JavaScript:
  1. function get_company()
  2. {
  3.     return databaseManager.hasRecords(contacts_to_companies) ? contacts_to_companies.getRecord(1) : null;
  4. }

Then, when you need to access the related record in code, use that calculation instead of reproducing the logic to fetch the related company....like:

JavaScript:
  1. var contact = foundset.getRecord(foundset.getSelectedIndex(); // fetch a contact from anywhere
  2. var company = contact.get_company;

I find this a very handy shortcut to have around and try to define a "get_xxx" method on all my child tables that allow me to fetch the related parent in the relation.

Wednesday, October 1st, 2008 at 2:45 pm and is filed under Servoy. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply