瀏覽代碼

Publish: re-use bundle rather then re-loading it.

Lacey Sanderson 6 年之前
父節點
當前提交
ecc9625d91

+ 19 - 14
tripal/includes/TripalEntityController.inc

@@ -45,7 +45,12 @@ class TripalEntityController extends EntityAPIController {
     $modules = module_implements('entity_create');
     foreach ($modules as $module) {
       $function = $module . '_entity_create';
-      $function($entity, $values['type']);
+      if (isset($values['bundle_object'])) {
+        $function($entity, $values['type'], $values['bundle_object']);
+      }
+      else {
+        $function($entity, $values['type']);
+      }
     }
     return $entity;
 
@@ -102,20 +107,20 @@ class TripalEntityController extends EntityAPIController {
    *   The entity whose title should be changed.
    * @param $title
    *   The title to use. It can contain tokens the correspond to field values.
-   *   Token should be be compatible with those returned by 
+   *   Token should be be compatible with those returned by
    *   tripal_get_entity_tokens().
    */
   public function setTitle($entity, $title = NULL) {
-    
+
     $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
-    
+
     // If no title was supplied then we should try to generate one using the
     // default format set by admins.
-    if (!$title) {     
+    if (!$title) {
       $title = tripal_get_title_format($bundle);
     }
     $title = tripal_replace_entity_tokens($title, $entity, $bundle);
-    
+
     if ($title) {
       db_update('tripal_entity')
         ->fields(array(
@@ -128,12 +133,12 @@ class TripalEntityController extends EntityAPIController {
 
   /**
    * Sets the URL alias for an entity.
-   * 
+   *
    * @param $entity
    *   The entity whose URL alias should be changed.
    * @param $alias
    *   The alias to use. It can contain tokens the correspond to field values.
-   *   Token should be be compatible with those returned by 
+   *   Token should be be compatible with those returned by
    *   tripal_get_entity_tokens().
    */
   public function setAlias($entity, $alias = NULL) {
@@ -155,17 +160,17 @@ class TripalEntityController extends EntityAPIController {
     // the term name and entity id.
     if (!$alias) {
 
-      // Load the term for this TripalEntity. Set a default based on the term 
-      // name and entity id. Then replace all the tokens with values from 
+      // Load the term for this TripalEntity. Set a default based on the term
+      // name and entity id. Then replace all the tokens with values from
       // the entity fields.
       $term = entity_load('TripalTerm', array('id' => $entity->term_id));
       $term = reset($term);
       $alias = str_replace(' ', '', $term->name) . '/[TripalEntity__entity_id]';
       $alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
     }
-    
-    // Check if the passed alias has tokens. Load the TripalBundle entity for 
-    // this TripalEntity. Then replace all the tokens with values from the 
+
+    // Check if the passed alias has tokens. Load the TripalBundle entity for
+    // this TripalEntity. Then replace all the tokens with values from the
     // entity fields.
     if($alias && (preg_match_all("/\[[^\]]*\]/", $alias, $bundle_tokens))) {
       $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
@@ -243,7 +248,7 @@ class TripalEntityController extends EntityAPIController {
           drupal_write_record('url_alias', $values);
         }
       }
-      // If there is only one alias matching then it might just be that we 
+      // If there is only one alias matching then it might just be that we
       // already assigned this alias to this entity in a previous save.
       elseif ($num_aliases == 1) {
 

+ 13 - 2
tripal_chado/api/tripal_chado.api.inc

@@ -259,22 +259,33 @@ function chado_publish_records($values, $job_id = NULL) {
         // multiple entities at once... sort of like the copy method.
         $record_id = $record->record_id;
         $ec = entity_get_controller('TripalEntity');
+
+        // @performance remove after development
+        // print 'After get controller :' . (microtime(true) - $started_at) . "s.\n";
+
         $entity = $ec->create(array(
           'bundle' => $bundle_name,
           'term_id' => $bundle->term_id,
           // Add in the Chado details for when the hook_entity_create()
           // is called and our tripal_chado_entity_create() implementation
           // can deal with it.
-          // @performance maybe there is something we can easily do here?
-          'chado_record' => chado_generate_var($table, array($pkey_field => $record_id)),
+          'chado_record' => chado_generate_var($table, array($pkey_field => $record_id), array('include_fk' => 0)),
           'chado_record_id' => $record_id,
           'publish' => TRUE,
+          'bundle_object' => $bundle,
         ));
+
+        // @performance remove after development
+        // print 'After tripal_chado_entity_create() :' . (microtime(true) - $started_at) . "s.\n";
+
         $entity = $entity->save();
         if (!$entity) {
           throw new Exception('Could not create entity.');
         }
 
+        // @performance remove after development
+        // print 'After entity save :' . (microtime(true) - $started_at) . "s.\n";
+
           // @performance remove after development: this takes 0.2-0.3s.
           //print 'Create entity itself :' . (microtime(true) - $started_at) . "s.\n";
 

+ 11 - 2
tripal_chado/includes/tripal_chado.entity.inc

@@ -7,8 +7,15 @@
  * This hook is called when brand new entities are created, but
  * they are not loaded so the hook_entity_load() is not yet called. We
  * can use this hook to add properties to the entity before saving.
+ *
+ * @param $entity
+ *   The entity being created.
+ * @param $type
+ *   The type of entity being created.
+ * @param $bundle (OPTIONAL)
+ *   The bundle object for the current entity.
  */
-function tripal_chado_entity_create(&$entity, $type) {
+function tripal_chado_entity_create(&$entity, $type, $bundle = NULL) {
   if ($type == 'TripalEntity') {
 
     // Set some defaults on vars needed by this module.
@@ -18,7 +25,9 @@ function tripal_chado_entity_create(&$entity, $type) {
       $entity->chado_linker = NULL;
 
       // Add in the Chado table information for this entity type.
-      $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+      if (!$bundle) {
+        $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+      }
       if ($bundle->data_table) {
         $entity->chado_table = $bundle->data_table;
         $entity->chado_column = $bundle->type_column;