Selaa lähdekoodia

Updated code to support new linking table

Stephen Ficklin 9 vuotta sitten
vanhempi
commit
82a085ca88

+ 6 - 0
tripal_entities/includes/TripalEntityController.inc

@@ -104,6 +104,9 @@ class TripalEntityController extends EntityAPIController {
       field_attach_update($entity->entity_type, $entity);
     }
 
+    // Invoke hook_entity_presave().
+    module_invoke_all('entity_presave', $entity, $entity->entity_type);
+
     // Write out the entity record.
     /* $tablename = 'feature';
     $schema = chado_get_schema($tablename);
@@ -124,6 +127,9 @@ class TripalEntityController extends EntityAPIController {
       $entity->id = $record['id'];
     }
 
+    // Invoke either hook_entity_update() or hook_entity_insert().
+    module_invoke_all($invocation, $entity, $entity->entity_type);
+
     return $entity;
   }
 }

+ 2 - 3
tripal_entities/includes/tripal_entities.admin.inc

@@ -81,12 +81,11 @@ function tripal_entities_content_overview_form($form, &$form_state) {
   $headers = array('Title', 'Vocabulary', 'Term', 'Author', 'Status', 'Updated', 'Operations');
   $rows = array();
   while ($entity = $entities->fetchObject()) {
-    $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $entity->cvterm_id));
     $author = user_load($entity->uid);
     $rows[] = array(
       l($entity->title, 'data/' . $entity->id),
-      $entity->type . ' (' . $cvterm->cv_id->name . ')',
-      $entity->bundle  . ' (' . $cvterm->name . ')',
+      $entity->type,
+      $entity->bundle,
       l($author->name, 'user/' . $entity->uid),
       $entity->status == 1 ? 'published' : 'unpublished',
       format_date($entity->changed, 'short'),

+ 0 - 2
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -5,9 +5,7 @@
  * Implements hook_entity_load().
  */
 function tripal_entities_entity_load($entities, $type) {
-  foreach ($entities as $id => $entity) {
 
-  }
 }
 
 /**

+ 38 - 6
tripal_entities/includes/tripal_entities.field_storage.inc

@@ -51,16 +51,34 @@ function tripal_entities_field_storage_write($entity_type, $entity, $op, $fields
   // Add in the type_id field.
   $values[$type_field] = $entity->cvterm_id;
 
+  $entity->storage = array();
   switch ($op) {
     case FIELD_STORAGE_INSERT:
       $record = chado_insert_record($tablename, $values);
       if ($record === FALSE) {
         drupal_set_message('Could not insert Chado record.', 'error');
       }
-      $entity->record_id = $record[$pkey_field];
+      $entity->storage['chado']['record_id'] = $record[$pkey_field];
+      $entity->storage['chado']['data_table'] = $tablename;
+      $entity->storage['chado']['type_table'] = $tablename;
+      $entity->storage['chado']['field'] = $type_field;
+
+      // Add a record to the chado_entity table so that the data for the
+      // fields can be pulled from Chado when loaded the next time.
+      $record = array(
+        'entity_id' => $entity->id,
+        'record_id' => $entity->storage['chado']['record_id'],
+        'data_table' => $entity->storage['chado']['data_table'],
+        'type_table' => $entity->storage['chado']['type_table'],
+        'field' => $entity->storage['chado']['field'],
+      );
+      $success = drupal_write_record('chado_entity', $record);
+      if (!$success) {
+        drupal_set_message('Unable to insert new data.', 'error');
+      }
       break;
     case FIELD_STORAGE_UPDATE:
-      $match[$pkey_field] = $entity->record_id;
+      $match[$pkey_field] = $entity->storage['chado']['record_id'];
       chado_update_record($tablename, $match, $values);
       break;
   }
@@ -77,12 +95,26 @@ function tripal_entities_field_storage_load($entity_type, $entities, $age, $fiel
   $langcode = $language->language;
 
   foreach ($entities as $id => $entity) {
+   // Get the base table and record id for the fields of this entity.
+   $details = db_select('chado_entity', 'ce')
+      ->fields('ce')
+      ->condition('entity_id', $entity->id)
+      ->execute()
+      ->fetchObject();
+    if (!$details) {
+      // TODO: what to do if record is missing!
+    }
+    $entity->storage['chado']['record_id'] = $details->record_id;
+    $entity->storage['chado']['data_table'] = $details->data_table;
+    $entity->storage['chado']['type_table'] = $details->type_table;
+    $entity->storage['chado']['field'] = $details->field;
+
     // Find out which table should receive the insert.
-    $tablename = 'feature';
-    $type_field = 'type_id';
+    $tablename = $entity->storage['chado']['data_table'];
+    $type_field = $entity->storage['chado']['field'];
     $schema = chado_get_schema($tablename);
     $pkey_field = $schema['primary key'][0];
-    $record_id = $entity->record_id;
+    $record_id = $entity->storage['chado']['record_id'];
 
     // Iterate through the field names to get the list of tables and fields
     // that should be queried.
@@ -104,7 +136,7 @@ function tripal_entities_field_storage_load($entity_type, $entities, $age, $fiel
     }
 
     // Get the record
-    $values = array($pkey_field => $entity->record_id);
+    $values = array($pkey_field => $record_id);
     $record = chado_select_record($tablename, $columns[$tablename], $values);
 
     // Now set the field values

+ 3 - 4
tripal_entities/tripal_entities.install

@@ -19,8 +19,6 @@ function tripal_entities_install() {
   // chado schema
   tripal_entities_add_custom_tables();
 
-  drupal_set_message('The current Entity Example (Genes) requires the Sequence Ontology.', 'warning');
-
 }
 
 /**
@@ -34,7 +32,8 @@ function tripal_entities_add_custom_tables() {
   tripal_entities_add_tripal_term_relationship_table();
 }
 
-/**
+/**The current Entity Example (Genes) requires the Sequence Ontology.                                     [warning]
+
  *
  */
 function tripal_entities_add_tripal_vocabulary_table() {
@@ -425,7 +424,7 @@ function tripal_entities_schema() {
       'bundle' => array('bundle'),
     ),
   );
-  
+
   $schema['chado_entity'] = array(
     'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
     'fields' => array(

+ 5 - 3
tripal_entities/tripal_entities.module

@@ -326,8 +326,10 @@ function tripal_bundle_load($bundle_type, $reset = FALSE) {
 }
 
 /**
- * Fetch a tripal_entity object. Make sure that the wildcard you choose
- * in the tripal_entity entity definition fits the function name here.
+ * Allows the menu system to use a wildcard to fetch the entity.
+ *
+ * Make sure that the wildcard you choose in the tripal_entity entity
+ * definition fits the function name here.
  *
  * @param $id
  *   Integer specifying the tripal_entity id.
@@ -348,6 +350,6 @@ function tripal_entity_load($id, $reset = FALSE) {
     ->fetchField();
 
   // Load the entity.
-  $entity =  entity_load($entity_type, array($id), array(), $reset);
+  $entity = entity_load($entity_type, array($id), array(), $reset);
   return reset($entity);
 }