Child pages
  • GetLinkedObject
Skip to end of metadata
Go to start of metadata


This document describes best practices for getting objects via the link tables in TUSK.

Throughout TUSK objects are linked together via link tables. These link tables are used to create relationships between objects.

Code Examples


Although a number of objects have built in methods to easily get related objects, there are instances where code needs to be written. The process is fairly simple, get a link definition, call it's get_parents method with the primary key of the child and get the parent objects from that list.

use HSDB4::SQLLink
my $linkdef = $HSDB4::SQLLinkDefinition::LinkDefs{"hsdb45_med_admin.link_course_user"};
my @courses = $linkdef->get_parents($self->primary_key())->parents();

Generic with Conditional

The above example returns all parents associated via the link table. In some instances a limited set of parents is wanted. We can accomplish that by passing in conditions.

use HSDB4::SQLLink
my $linkdef = $HSDB4::SQLLinkDefinition::LinkDefs{"hsdb45_med_admin.link_course_user"};
my @courses = $linkdef->get_parents($self->primary_key(),"course_id > 50")->parents();

The condition can be explicitly applied to the parent or link table by prepending "parent." or "link." to the field name.

List of User Courses

Relationships between courses and users are maintained in link_course_user. To get an array of course objects for a particular course we use a method in the user object:

use HSDB4::SQLRow::User;

my $user = HSDB4::SQLRow::User->new;
@courses = $user->parent_courses();

This gives us an array of course objects which can be looped over etc.

List of User Courses with Conditions

The above example returns all courses associated with a user in link_course_user, but there are times when only a limited set of courses is wanted. On these occasions we can pass a condition to the parent_course method:

use HSDB4::SQLRow::User;

my $user = HSDB4::SQLRow::User->new;
@courses = $user->parent_courses("course_id > 500");

There are two tables involved in this action; course and link_course_user. In most instances throughout the system the condition field appears in only one of the tables, and doesn't need to be specified. For clarity, one can prepent "parent" or "link" to the conditional to specify which table that condition should be applied.

@courses = $user->parent_courses("parent.course_id > 500");
  • No labels