You are here

public function RelationsImportSettingsBase::relationExists in CRM Core 7

Check if a relation exists between two contacts.

Parameters

CRMCoreContactEntity $source_entity: Fully loaded source contact.

CRMCoreContactEntity $destination_entity: Fully loaded source contact.

string $relation_type: Relation type.

Return value

int|NULL Relation ID if it exists, NULL otherwise.

1 call to RelationsImportSettingsBase::relationExists()
RelationsImportSettingsBase::createRelation in modules/crm_core_data_import/plugins/settings/RelationsImportSettingsBase.inc
Create relation between contacts.

File

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

Class

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

Code

public function relationExists(CRMCoreContactEntity $source_entity, CRMCoreContactEntity $destination_entity, $relation_type) {
  $entity_type = 'crm_core_contact';
  $query = db_select('field_data_endpoints', 'endpoint_src');
  $query
    ->innerJoin('field_data_endpoints', 'endpoint_dst', "endpoint_src.entity_type = endpoint_dst.entity_type AND endpoint_src.bundle = endpoint_dst.bundle AND endpoint_src.deleted = endpoint_dst.deleted AND endpoint_src.entity_id = endpoint_dst.entity_id AND endpoint_src.endpoints_entity_type = endpoint_dst.endpoints_entity_type");
  $query
    ->condition('endpoint_src.entity_type', 'relation')
    ->condition('endpoint_src.bundle', $relation_type)
    ->condition('endpoint_src.deleted', 0)
    ->condition('endpoint_src.endpoints_entity_type', $entity_type)
    ->condition('endpoint_src.endpoints_entity_id', $source_entity->contact_id)
    ->condition('endpoint_dst.endpoints_entity_id', $destination_entity->contact_id)
    ->fields('endpoint_src', array(
    'entity_id',
  ));
  return $query
    ->execute()
    ->fetchField();
}