You are here

function crm_core_relationship_uninstall in CRM Core 8.3

Same name and namespace in other branches
  1. 8 modules/crm_core_relationship/crm_core_relationship.install \crm_core_relationship_uninstall()
  2. 8.2 modules/crm_core_relationship/crm_core_relationship.install \crm_core_relationship_uninstall()
  3. 7 modules/crm_core_relationship/crm_core_relationship.install \crm_core_relationship_uninstall()

Implements hook_uninstall().

File

modules/crm_core_relationship/crm_core_relationship.install, line 50
Install, update and uninstall functions for the relationship module.

Code

function crm_core_relationship_uninstall() {

  // Get all the relationship_types (bundles), find all fields
  // delete them.
  $query = db_select('relation_bundles', 'rb')
    ->fields('rb', [
    'relation_type',
  ])
    ->condition('rb.entity_type', 'crm_core_contact')
    ->distinct()
    ->execute();
  while ($record = $query
    ->fetchAssoc()) {
    $relationship_type[] = $record['relation_type'];
  }
  foreach ($relationship_type as $type) {

    // Look into the database for each type.
    $relationship_query = db_select('relation', 'r')
      ->fields('r', [
      'relation_id',
    ])
      ->condition('r.relation_type', $type)
      ->execute();
    while ($result = $relationship_query
      ->fetchAssoc()) {

      // Delete all the relationships of that type.
      Relation::load($result['relation_id'])
        ->delete();
    }

    // Finally delete the relationshp_type.
    RelationType::load($type)
      ->delete();
  }
}