You are here

function relation_relation_exists in Relation 7

Checks if a relation exists.

The following example demonstrates how to check if a relation of type 'likes' exists between two entities, user 17 and node 253.

$entity_keys = array(
  array(
    'entity_type' => 'user',
    'entity_id' => 17,
  ),
  array(
    'entity_type' => 'node',
    'entity_id' => 253,
  ),
);
$relation_type = 'likes';
$results = relation_relation_exists($entity_keys, $relation_type);

Parameters

$entity_keys: The entity keys of the relation to found. Entity_keys are arrays keyed by 'entity_type' and 'entity_id'.

$relation_type: (Optional) The relation type (bundle) of the relation to be checked.

$enforce_direction: (Optional) Whether to enforce direction as specified in $entity_keys.

Return value

Return FALSE if no relation exists, or an array of matching relations.

4 calls to relation_relation_exists()
FeedsCSVtoRelationsTest::testUniqueEndpoints in relation_feeds/tests/relation.feeds_processor.test
Test unique endpoints setting
RelationAPITestCase::testRelationQuery in tests/relation.test
Tests all available methods in RelationQuery. Creates some nodes, add some relations and checks if they are related.
RelationFeedsProcessor::existingEntityId in relation_feeds/RelationFeedsProcessor.inc
relation_endpoint_field_validate in ./relation_endpoint.module
Implements hook_field_validate().

File

./relation.module, line 532
Describes relations between entities.

Code

function relation_relation_exists($entity_keys, $relation_type = NULL, $enforce_direction = FALSE) {
  $query = relation_query();
  foreach ($entity_keys as $r_index => $entity_key) {
    $query
      ->related($entity_key['entity_type'], $entity_key['entity_id'], $enforce_direction ? $r_index : NULL);
  }
  if ($relation_type) {
    $query
      ->propertyCondition('relation_type', $relation_type);
  }
  $query
    ->propertyCondition('arity', count($entity_keys));

  // Avoid Node Access restrictions when checking for a relation
  $query
    ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');

  // If direction of the relation is not forced make sure the each endpoint
  // is counted just once.
  if (!$enforce_direction) {
    $query
      ->addTag('enforce_distinct_endpoints');
  }
  $relation_ids = $query
    ->execute();
  return $relation_ids ? $relation_ids : FALSE;
}