Accessing Linked Records

As mentioned previously, FK relationships are automatically expanded within the object when they are defined in the table housing the base data type.  For example, the organism_id and type_id FK relationships are defined in the feature table (the base table). Additionally, some foreign-key relationships are defined in other tables besides the base table.  For example, the feature_synonym table defines an FK relationships with the feature table.  Thus, adding information from a one-to-many relationship is performed using the same chado_expand_var() function. Continuing with the feature synonym example, a feature may have more than one synonym (one-to-many). The synonyms for a feature are stored in the feature_synonym table of Chado. We can therefore obtain the list of synonyms for a feature with the following example code:

$feature = $variables['node']->feature;
$feature = chado_expand_var($feature, 'table', 'feature_synonym');

Notice in the code the second argument is table to indicate we want to expand the record to include a new table, and the third argument provides the name of the table: feature_synonym.

Unlike field expansion, records from a one-to-many relationship are accessed using the name of the table. For example:

$synonyms = $feature->feature_synonym;

The results from a one-to-many relationship are available as a single value if only one record was returned or as an array if multiple records were returned. You can force the function to always return results as an array by providing an option array with the  return_array key:

$options = array('return_array' => 1);
$feature = tripal_core_expand_chado_vars($feature, 'table', 'feature_synonym', $options);
$synonyms = $feature->feature_synonym;