You are here

public function RelationsImportSettingsBase::createRelation in CRM Core 7

Create relation between contacts.

Parameters

object $source_entity: Source entity.

object $destination_entity: Target entity.

string $relation_type: Type of relation.

2 calls to RelationsImportSettingsBase::createRelation()
RelationsCiviCrmImportSettings::postImport in modules/crm_core_data_import/plugins/settings/RelationsCiviCrmImportSettings.inc
Performs when import was successful.
RelationsImportSettings::postImport in modules/crm_core_data_import/plugins/settings/RelationsImportSettings.inc
Performs when import was successful.

File

modules/crm_core_data_import/plugins/settings/RelationsImportSettingsBase.inc, line 226
Base relations settings handler for CRM Core Data Import.

Class

RelationsImportSettingsBase
@file Base relations settings handler for CRM Core Data Import.

Code

public function createRelation($source_entity, $destination_entity, $relation_type) {
  $source_entity = entity_load_single($source_entity->entity_type, $source_entity->id);
  $destination_entity = entity_load_single($destination_entity->entity_type, $destination_entity->id);
  if ($source_entity && $destination_entity) {

    // We cannot rely on relation_get_related_entity() because as stated in
    // documentation "Do not expect to get exactly what you want, especially
    // if you have multiple relations of the same type on the search entity."
    // We need to check more precisely here.
    if (!$this
      ->relationExists($source_entity, $destination_entity, $relation_type)) {
      $endpoints = array(
        0 => array(
          'entity_type' => 'crm_core_contact',
          'entity_id' => $source_entity->contact_id,
          'entity_bundle' => $source_entity->type,
          'r_index' => 0,
          'entity_key' => $source_entity->type . ': ' . $source_entity->contact_id,
        ),
        1 => array(
          'entity_type' => 'crm_core_contact',
          'entity_id' => $destination_entity->contact_id,
          'entity_bundle' => $destination_entity->type,
          'r_index' => 1,
          'entity_key' => $destination_entity->type . ': ' . $destination_entity->contact_id,
        ),
      );
      $relation = relation_create($relation_type, $endpoints);
      relation_save($relation);
    }
  }
}