function tripal_core_insert_property
2.x tripal_core.DEPRECATED.api.inc | tripal_core_insert_property($basetable, $record_id, $property,
$cv_name, $value, $update_if_present = 0) |
3.x tripal_core.DEPRECATED.inc | tripal_core_insert_property($basetable, $record_id, $property,
$cv_name, $value, $update_if_present = 0) |
1.x tripal_core_chado.api.inc | tripal_core_insert_property($basetable, $record_id, $property,
$cv_name, $value, $update_if_present = 0) |
Insert a property for a given base table. By default if the property already exists a new property is added with the next available rank. If $update_if_present argument is specified then the record will be updated if it exists rather than adding a new property.
Parameters
$basetable: The base table for which the property should be inserted. Thus to insert a property for a feature the basetable=feature and property is inserted into featureprop
$record_id: The foriegn key value of the base table. This should be in integer.
$property: The cvterm name describing the type of properties to be inserted
$cv_name: The name of the cv that the above cvterm is part of
$value: The value of the property to be inserted (can be empty)
$update_if_present: A boolean indicating whether an existing record should be updated. If the property already exists and this value is not specified or is zero then a new property will be added with the next largest rank.
Return value
Return True on Insert/Update and False otherwise
Related topics
- tripal_analysis_insert_property in tripal_analysis/
api/ tripal_analysis.api.inc - Insert a given property
- tripal_contact_insert_property in tripal_contact/
api/ tripal_contact.api.inc - Insert a given property
- tripal_core_update_property in tripal_core/
api/ tripal_core_chado.api.inc - Update a property for a given base table record and property name. This function should be used only if one record of the property will be present. If the property name can have multiple entries (with increasing rank) then use the function named…
- tripal_featuremap_insert_property in tripal_featuremap/
api/ tripal_featuremap.api.inc - Insert a given property
- tripal_feature_analysis_insert_property in tripal_feature/
api/ tripal_feature.api.inc - Insert a property for an analysis feature
File
- tripal_core/
api/ tripal_core_chado.api.inc, line 2621 - The Tripal Core API
Code
function tripal_core_insert_property($basetable, $record_id, $property,
$cv_name, $value, $update_if_present = 0) {
// first see if the property already exists, if the user want's to update
// then we can do that, but otherwise we want to increment the rank and
// insert
$props = tripal_core_get_property($basetable, $record_id, $property, $cv_name);
if (!is_array($props) and $props) {
$props = array($props);
}
$rank = 0;
if (count($props) > 0) {
if ($update_if_present) {
return tripal_core_update_property($basetable, $record_id, $property, $cv_name, $value);
}
else {
// iterate through the properties returned and check to see if the
// property with this value already exists if not, get the largest rank
// and insert the same property but with this new value
foreach ($props as $p) {
if ($p->rank > $rank) {
$rank = $p->rank;
}
if (strcmp($p->value, $value) == 0) {
return TRUE;
}
}
// now add 1 to the rank
$rank++;
}
}
// make sure the cvterm exists. Otherwise we'll get an error with
// prepared statements not matching
$values = array(
'cv_id' => array(
'name' => $cv_name,
),
'name' => $property,
);
$options = array('statement_name' => 'sel_cvterm_cv');
$term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
if (!$term or count($term) == 0) {
watchdog('tripal_core', "Cannot find property '%prop_name'.",
array('%prop_name' => $property), WATCHDOG_ERROR);
return FALSE;
}
// get the foreign key for this property table
$table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
$fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
// construct the array of values to be inserted
$values = array(
$fkcol => $record_id,
'type_id' => array(
'cv_id' => array(
'name' => $cv_name,
),
'name' => $property,
),
'value' => $value,
'rank' => $rank,
);
$options = array('statement_name' => 'ins_' . $basetable . 'prop_' . substr($fkcol, 0, 2) . 'tyvara');
$result = tripal_core_chado_insert($basetable . 'prop', $values, $options);
return $result;
}