Эх сурвалжийг харах

Changed relationship template for faster loading

spficklin 12 жил өмнө
parent
commit
741b84fbef

+ 37 - 55
tripal_stock/tripal_stock.module

@@ -972,87 +972,69 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
   // we want to provide a new variable that contains the matched stocks.
   $stock = $variables['node']->stock;
    
-  // expand the stock object to include the stock relationships.
-  $options = array(
-    'order_by'=> array('rank' => 'ASC'),
-    'return_array' => 1,
-    'statement_name' => 'exp_stock_rels'
-  );
-  $stock = tripal_core_expand_chado_vars($stock,'table', 'stock_relationship', $options);
-
-  // get the subject relationships
-  $srelationships = $stock->stock_relationship->subject_id;
-  if (!$srelationships) {
-     $srelationships = array();
-  } 
-  elseif (!is_array($srelationships)) { 
-     $srelationships = array($srelationships); 
-  }
-  
-  // get the object relationships
-  $orelationships = $stock->stock_relationship->object_id;
-  if (!$orelationships) {
-     $orelationships = array();
-  } 
-  elseif (!is_array($orelationships)) { 
-     $orelationships = array($orelationships); 
-  }
+  // normally we would use tripal_core_expand_chado_vars to expand our
+  // organism object and add in the relationships, however whan a large
+  // number of relationships are present this significantly slows the
+  // query, therefore we will manually perform the query
+  $sql = "
+    SELECT S.name, S.stock_id, CS.nid, CVT.name as rel_type, CVTs.name as obj_type
+    FROM stock_relationship SR
+      INNER JOIN stock S on SR.object_id = S.stock_id
+      INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id
+      INNER JOIN cvterm CVTs on S.type_id = CVTs.cvterm_id
+      LEFT JOIN chado_stock CS on S.stock_id = CS.stock_id
+    WHERE SR.subject_id = %d      
+  ";
+  $as_subject = chado_query($sql, $stock->stock_id);
+  $sql = "
+    SELECT S.name, S.stock_id, CS.nid, CVT.name as rel_type, CVTs.name as sub_type
+    FROM stock_relationship SR
+      INNER JOIN stock S on SR.subject_id = S.stock_id
+      INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id
+      INNER JOIN cvterm CVTs on S.type_id = CVTs.cvterm_id
+      LEFT JOIN chado_stock CS on S.stock_id = CS.stock_id
+    WHERE SR.object_id = %d      
+  ";
+  $as_object = chado_query($sql, $stock->stock_id);   
   
   // combine both object and subject relationshisp into a single array
   $relationships = array();
   $relationships['object'] = array();
   $relationships['subject'] = array();
   
-   // iterate through the object relationships
-   foreach ($orelationships as $relationship) {
-     $rel = new stdClass();      
-     $rel->record = $relationship;    
+  // iterate through the object relationships
+  while ($relationship = db_fetch_object($as_object)) {
      
      // get the relationship and child types
-     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
-     $child_type = $relationship->subject_id->type_id->name;
+     $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
+     $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
      
-     // get the node id of the subject
-     $sql = "SELECT nid FROM chado_stock WHERE stock_id = %d";
-     $n = db_fetch_object(db_query($sql,$relationship->subject_id->stock_id));
-     if($n){
-        $rel->record->nid = $n->nid;
-     }
-
      if (!array_key_exists($rel_type, $relationships['object'])) {
        $relationships['object'][$rel_type] = array();   
      }
      if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
-       $relationships['object'][$rel_type][$child_type] = array();   
+       $relationships['object'][$rel_type][$sub_type] = array();   
      }
-     $relationships['object'][$rel_type][$child_type][] = $rel;     
+     $relationships['object'][$rel_type][$sub_type][] = $relationship;     
   }
-
   
   // now add in the subject relationships
-  foreach ($srelationships as $relationship) {
-     $rel = new stdClass(); 
-     
-     $rel->record = $relationship;
-     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
-     $parent_type = $relationship->object_id->type_id->name;
+  while ($relationship = db_fetch_object($as_subject)) {
      
-     // get the node id of the subject
-     $sql = "SELECT nid FROM chado_stock WHERE stock_id = %d";
-     $n = db_fetch_object(db_query($sql,$relationship->object_id->stock_id));
-     if($n){
-        $rel->record->nid = $n->nid;
-     }
+     // get the relationship and child types
+     $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
+     $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
      
      if (!array_key_exists($rel_type, $relationships['subject'])) {
        $relationships['subject'][$rel_type] = array();   
      }
      if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
-       $relationships['subject'][$rel_type][$parent_type] = array();   
+       $relationships['subject'][$rel_type][$obj_type] = array();   
      }
-     $relationships['subject'][$rel_type][$parent_type][] = $rel;
+     $relationships['subject'][$rel_type][$obj_type][] = $relationship;     
   }
   
+  
   $stock->all_relationships = $relationships;
 
 }