Browse Source

Fixing delete issue

Stephen Ficklin 9 years ago
parent
commit
99c141b34f

+ 1 - 1
tripal_entities/includes/TripalEntityController.inc

@@ -136,7 +136,7 @@ class TripalEntityController extends EntityAPIController {
     catch (Exception $e) {
       $transaction->rollback();
       watchdog_exception('tripal_core', $e);
-      drupal_set_message("Could not create the entity.", "error");
+      drupal_set_message("Could not save the entity:" . $e->getMessage(), "error");
     }
   }
 }

+ 19 - 9
tripal_entities/includes/tripal_entities.field_storage.inc

@@ -32,7 +32,6 @@ function tripal_entities_field_storage_write($entity_type, $entity, $op, $fields
     $residues = $field_vals['feature__residues'];
     $field_vals['feature__md5checksum'] = $field_vals['feature__md5checksum'] ? md5($residues) : NULL;
   }
-
   switch ($op) {
     case FIELD_STORAGE_INSERT:
       // Use the cvterm_id to look up tables where this term is used
@@ -80,6 +79,9 @@ function tripal_entities_field_storage_write($entity_type, $entity, $op, $fields
 function tripal_entities_field_storage_write_recursive($entity_type, $entity,
    $op, $field_vals, $tablename, $type_field = NULL, $record_id = NULL, $depth = 0) {
 
+  dpm(array($record_id, $tablename, $field_vals));
+
+
   // Intialize the values array and $record_id;
   $values = array();
 
@@ -105,15 +107,21 @@ function tripal_entities_field_storage_write_recursive($entity_type, $entity,
       // loop through the $fk_fields again.
       $fkey_fields_list[] = $local_id;
 
-      // Recurse on the FK field.  Pass in the ID for the FK field if one
-      // exists in the $field_vals;
+      // Get the value of the FK field as provided by the user.
       $fk_val = NULL;
       $fk_field_name = $tablename . '__' . $local_id;
       $fk_val = array_key_exists($fk_field_name, $field_vals) ? $field_vals[$fk_field_name] : NULL;
-      $fk_val = tripal_entities_field_storage_write_recursive($entity_type,
-        $entity, $op, $field_vals, $fk_table, NULL, $fk_val, $depth + 1);
-      if (!isset($fk_val) or $fk_val != '') {
-        $values[$local_id] = $fk_val;
+
+      // If the value of the field is 0 then the user specifically set this
+      // value to be ignore (i.e in a select box). Don't recurse.
+      if ($fk_val != 0) {
+        // Recurse on the FK field.  Pass in the ID for the FK field if one
+        // exists in the $field_vals;
+        $fk_val = tripal_entities_field_storage_write_recursive($entity_type,
+          $entity, $op, $field_vals, $fk_table, NULL, $fk_val, $depth + 1);
+        if (isset($fk_val) and $fk_val != '') {
+          $values[$local_id] = $fk_val;
+        }
       }
     }
   }
@@ -147,19 +155,21 @@ function tripal_entities_field_storage_write_recursive($entity_type, $entity,
     }
   }
 
+  dpm($values);
   // STEP 3: Insert/Update the record.
   // If there are no values then return.
   if (count($values) == 0) {
     return $record_id;
   }
   // If we don't have an incoming record ID then this is an insert.
-  if (!$record_id) {
+  if ($record_id == NULL) {
     // STEP 3a: Before inserting, we want to make sure the record does not
     // already exist.  Using the unique constraint check for a matching record.
     $options = array('is_duplicate' => TRUE);
     $is_duplicate = chado_select_record($tablename, array('*'), $values, $options);
     if($is_duplicate) {
-      throw new Exception('Could not insert Chado record into table: "' . $tablename . '".');
+      $record = chado_select_record($tablename, array('*'), $values);
+      return $record[0]->$pkey_field;
     }
 
     // STEP 3b: Insert the reocrd

+ 23 - 6
tripal_entities/includes/tripal_entities.fields.inc

@@ -312,13 +312,12 @@ function tripal_entities_primary_dbxref_widget_validate($element, &$form_state)
   if (count($db_id) > 0 and count($accession) == 0) {
     form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
   }
-  
-  // If user did not select a database, we want to remove dbxref_id from the 
-  // feature table but keep the dbxref record
+
+  // If user did not select a database, we want to remove dbxref_id from the
+  // table, we can do this by changing the '0' field value to a __NULL__
+  // which the Tripal Chado API understands as a NULL value.
   if (count($db_id) == 0) {
-    foreach ($form_state['values'][$field_name] as $langcode => $items) {
-      $form_state['values']['feature__dbxref_id'][$langcode][0]['value'][0]['feature__dbxref_id'] = '__NULL__';
-    }
+    tripal_entities_set_field_form_values($field_name, $form_state, '', 'dbxref__dbxref_id');
   }
 }
 /**
@@ -370,3 +369,21 @@ function tripal_entities_get_field_form_values($field_name, $form_state, $child
   }
   return $values;
 }
+
+/**
+ * Returns the values of the field from the $form_state.
+ */
+function tripal_entities_set_field_form_values($field_name, &$form_state, $value, $child = NULL) {
+  $values = array();
+  foreach ($form_state['values'][$field_name] as $langcode => $items) {
+    foreach ($items as $delta => $value) {
+      if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
+        $form_state['values'][$field_name][$langcode][$delta]['value'][0][$child] = $value;
+      }
+      else if (!$child and $value['value']) {
+        $form_state['values'][$field_name][$langcode][$delta]['value'] = $value;
+      }
+    }
+  }
+  return $values;
+}