How to programmatically delete a model

Not applicable

If I wanted to programmatically delete a model, its taxonomy, and its resources, what function do I use and what variables do I need to pass in?

I was looking at smartdocs_model_delete from \profiles\apigee\modules\custom\devconnect\smartdocs\smartdocs.admin.inc but it is used in the UI and would require a form_state variable.

Solved Solved
1 5 174
1 ACCEPTED SOLUTION

@Mitchel Skees Please try below code, Should work, Keep me posted.

function smartdocs_model_custom_delete($model_id) {
  $model_array = smartdocs_model_load($model_id);
  $model = new Model(devconnect_default_org_config());
  Model::fromArray($model, $model_array);
  $model_name = $model->getName();
  $model_uuid = $model->getUuid();
  try {
    $start = microtime(TRUE);
    $model->delete();
    if (variable_get('smartdocs_log_transaction_time', 0)) {
      $args = array(
        '!model' => $model_uuid,
        '!time' => number_format(microtime(TRUE) - $start, 4),
      );
      watchdog('smartdocs', 'Deleted model !model in !time seconds', $args, WATCHDOG_INFO);
    }
    // Delete all nodes in this model.
    $model_terms = taxonomy_get_term_by_name($model_name, 'smartdocs_models');
    if (!empty($model_terms)) {
      $model_term = reset($model_terms);
      $query = new EntityFieldQuery();
      $result = $query->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', 'smart_method')
        ->fieldCondition('field_smart_method_model', 'tid', $model_term->tid, '=')
        ->execute();
      $nids = array_keys($result['node']);
      if (!empty($nids)) {
        db_delete('smartdata')
          ->condition('nid', $nids)
          ->execute();
        node_delete_multiple($nids);
      }
      taxonomy_term_delete($model_term->tid);
    }
    // Remove any lingering views.
    $view_name = $model_name . '_methods';
    $view = views_get_view($view_name);
    if ($view) {
      views_delete_view($view);
    }
    // Make sure ctools is not caching the old view.
    db_delete('ctools_object_cache')->condition('name', $view_name)->execute();
    menu_rebuild();
    drupal_flush_all_caches();
    drupal_set_message(t('%model has been deleted along with all of its associated content.', array('%model' => $model->getDisplayName())), 'status');
    drupal_set_message('Successfully deleted model ' . $model->getName());
  }
  catch (Exception $e) {
    drupal_set_message('There was an error deleting the model(s).', 'error');
  }
}

Hope it helps.

View solution in original post

5 REPLIES 5

@Mitchel Skees Please try below code, Should work, Keep me posted.

function smartdocs_model_custom_delete($model_id) {
  $model_array = smartdocs_model_load($model_id);
  $model = new Model(devconnect_default_org_config());
  Model::fromArray($model, $model_array);
  $model_name = $model->getName();
  $model_uuid = $model->getUuid();
  try {
    $start = microtime(TRUE);
    $model->delete();
    if (variable_get('smartdocs_log_transaction_time', 0)) {
      $args = array(
        '!model' => $model_uuid,
        '!time' => number_format(microtime(TRUE) - $start, 4),
      );
      watchdog('smartdocs', 'Deleted model !model in !time seconds', $args, WATCHDOG_INFO);
    }
    // Delete all nodes in this model.
    $model_terms = taxonomy_get_term_by_name($model_name, 'smartdocs_models');
    if (!empty($model_terms)) {
      $model_term = reset($model_terms);
      $query = new EntityFieldQuery();
      $result = $query->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', 'smart_method')
        ->fieldCondition('field_smart_method_model', 'tid', $model_term->tid, '=')
        ->execute();
      $nids = array_keys($result['node']);
      if (!empty($nids)) {
        db_delete('smartdata')
          ->condition('nid', $nids)
          ->execute();
        node_delete_multiple($nids);
      }
      taxonomy_term_delete($model_term->tid);
    }
    // Remove any lingering views.
    $view_name = $model_name . '_methods';
    $view = views_get_view($view_name);
    if ($view) {
      views_delete_view($view);
    }
    // Make sure ctools is not caching the old view.
    db_delete('ctools_object_cache')->condition('name', $view_name)->execute();
    menu_rebuild();
    drupal_flush_all_caches();
    drupal_set_message(t('%model has been deleted along with all of its associated content.', array('%model' => $model->getDisplayName())), 'status');
    drupal_set_message('Successfully deleted model ' . $model->getName());
  }
  catch (Exception $e) {
    drupal_set_message('There was an error deleting the model(s).', 'error');
  }
}

Hope it helps.

Hey @Anil Sagar,

If I had the tid of a model, how could I use that to quickly get the model ID to pass into the function you provided?

From the code I have available, I would have to load all models and search the model names for the one that matches the term name. Then I would load the model and retrieve the model ID.

<?php
/**
 * Custom function to retrieve the API model by tid
 * 
 * 
 * Input: $tid (taxonomy term tid field)
 * Output: $model (API model object)
 */
function retrieve_model_by_tid($tid) {
  //Load the model with the matching tid
  $term = taxonomy_term_load($tid);
  //Get name of the model
  $model_name = $term->name;
  
  //Setup a list of all Apigee models
  $default_model = new Model(devconnect_default_org_config());
  $models = $default_model->listModels();

  //Search through models and see if the name matches - if there is a match return the model and stop
  foreach ($models as $model) {
    if ($model->getName() == $model_name) {
      $model->load($model->getUuid());
      return $model;
    }
  }
  //No match return null
  return null;
}

$loaded_model = retrieve_model_by_tid($some_tid_number);
smartdocs_model_custom_delete($loaded_model->getUUID());

This seems really inefficient and I was wondering if you had a quicker cleaner way.

Thank you for your time,

Mitchel

@Mitchel Skees , I don't think any reference is maintained in drupal db to directly query. I think above method is the one you should use if you would like to delete smartdocs by tid.

Thanks @Anil Sagar! Looks like that is the solution I will go with.

Also I would like to give credit to @Tamás Nagy. Most of the code I used was based on his work.

@Mitchel Skees , smartdocs_model_delete_submit function uses only $model_array, which you can populate & reuse rest of the code in smartdocs_model_delete_submit function. See custom function implementation in below answer which does same. All you need to do is, Input model id to below custom function. Hope it helps.