function tripal_bulk_loader_add_foreignkey_to_values
2.x tripal_bulk_loader.loader.inc | tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority, $default_data) |
3.x tripal_bulk_loader.loader.inc | tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority, $default_data) |
1.x tripal_bulk_loader.loader.inc | tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority, $default_data) |
Handles foreign keys in the values array.
Specifically, if the value for a field is an array then it is assumed that the array contains the name of the record whose values array should be substituted here. Thus the foreign record is looked up and the values array is substituted in.
Related topics
1 call to tripal_bulk_loader_add_foreignkey_to_values()
- process_data_array_for_line in tripal_bulk_loader/
includes/ tripal_bulk_loader.loader.inc - Process the data array for a given line
File
- tripal_bulk_loader/
includes/ tripal_bulk_loader.loader.inc, line 909 - Handles the actual loading of data.
Code
function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority, $default_data) {
// iterate through each field in the $values arrray and
// substitute any values for FK / referring fields
foreach ($values as $field => $value) {
// if the field value is an array then it is an FK
if (is_array($value)) {
// get the name and priority of the foreign record
$foreign_record = $value['foreign record']['record'];
$foreign_priority = $record2priority[$foreign_record];
$foreign_table = $value['foreign record']['table'];
$foreign_field = $value['foreign record']['field'];
// get the values of the foreign record and substitute those for the values
$foreign_values = $data[$foreign_priority]['values_array'];
// check to see if we have any default values in the $default_data array
// these were populated from select statements that only need to run once
// so we can reuse the values from those previous selects.
if (array_key_exists($foreign_priority, $default_data) and
array_key_exists('values_default', $default_data[$foreign_priority]) and
array_key_exists($foreign_field, $default_data[$foreign_priority]['values_default'])) {
$values[$field] = $default_data[$foreign_priority]['values_default'][$foreign_field];
continue;
}
// if the field in the Referral records is in a FK relationship with
// this field then we can simply keep the value we have
$tbl_description = chado_get_schema($table_array['table']);
if ($tbl_description and
array_key_exists('foreign keys', $tbl_description) and
array_key_exists($foreign_table, $tbl_description['foreign keys']) and
array_key_exists($field, $tbl_description['foreign keys'][$foreign_table]['columns']) and
$foreign_field == $tbl_description['foreign keys'][$foreign_table]['columns'][$field]) {
$values[$field] = $foreign_values;
}
// if the field in the Referral records is not in an FK relationship
// with this field then we we have to get the requested value, we must
// return only a single value
else {
// if the current value of the referral records is a non-array then this
// is the primary key, we can use it to select the value we need.
$fk_description = chado_get_schema($foreign_table);
if (!is_array($foreign_values)) {
// if we have a value then use it to get the field we need
if ($foreign_values) {
$fvalues = array($fk_description['primary key'][0] => $foreign_values);
$columns = array($foreign_field);
$options = array('statement_name' => 'pk_' . $foreign_table);
$options['print_errors'] = TRUE;
$record = chado_select_record($foreign_table, $columns, $fvalues, $options);
if ($record) {
$values[$field] = $record[0]->$foreign_field;
}
else {
unset($values[$field]);
}
}
// if we don't have a value then there's nothing we can do so
// set this value to nothing as well
else {
unset($values[$field]);
}
}
// if the current value is an array and our field is not in it, then
// we need to select a value for our field.
else {
$fvalues = $foreign_values;
$columns = array($foreign_field);
$options = array('statement_name' => 'blk_' . $nid . $priority . $foreign_table);
$options['print_errors'] = TRUE;
$record = chado_select_record($foreign_table, $columns, $fvalues, $options);
if ($record) {
$values[$field] = $record[0]->$foreign_field;
}
else {
unset($values[$field]);
}
} // end else from: if (!is_array($foreign_values) ...
} // end else from: if ($tbl_description ...
} // end if(is_array($value)) ...
} // end foreach ($values ...
// return the updated field values
return $values;
}