You are here

function relation_save in Relation 7

Saves a relation.

Parameters

$relation: The relation entity data object. See relation_create() for the appropriate format (or just use it).

Return value

The new relation id.

12 calls to relation_save()
MigrateDestinationRelation::import in relation_migrate/relation_migrate.destination.inc
Import a single relation.
RelationAPITestCase::testRelationRevision in tests/relation.test
Tests relation revisions.
RelationAPITestCase::testRelationSave in tests/relation.test
Tests saving of relations.
RelationFeedsProcessor::entitySave in relation_feeds/RelationFeedsProcessor.inc
RelationTestCase::saveRelation in tests/relation.test
Saves a relation.

... See full list

2 string references to 'relation_save'
MigrateDestinationRelation::import in relation_migrate/relation_migrate.destination.inc
Import a single relation.
relation_entity_info in ./relation.module
Implements hook_entity_info().

File

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

Code

function relation_save($relation, $account = NULL) {
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }
  try {
    field_attach_validate('relation', $relation);
  } catch (FieldValidationException $e) {
    foreach ($e->errors as $field_name => $field_errors) {
      foreach ($field_errors as $langcode => $multiple_errors) {
        foreach ($multiple_errors as $delta => $item_errors) {
          foreach ($item_errors as $item_error) {
            watchdog_exception('relation', $e, $item_error['message']);
          }
        }
      }
    }
    return FALSE;
  }

  // Determine if we will be inserting a new relation.
  if (!isset($relation->is_new)) {
    $relation->is_new = empty($relation->rid);
  }
  $transaction = db_transaction();
  $endpoints = field_get_items('relation', $relation, 'endpoints');
  $relation->arity = count($endpoints);

  // use time() instead of REQUEST_TIME, because otherwise tests
  // RelationQuery::order() are impossible.
  $relation->changed = time();
  if (!$relation->is_new) {
    $keys = array(
      'rid',
    );
    $op = 'update';
    $relation->original = relation_load($relation->rid, $relation->vid);
  }
  else {
    $relation->created = $relation->changed;
    $keys = array();
    $op = 'insert';
  }
  field_attach_presave('relation', $relation);
  module_invoke_all('entity_presave', $relation, 'relation');
  unset($relation->vid);
  $temp_uid = $relation->uid;
  $relation->uid = $account->uid;
  drupal_write_record('relation_revision', $relation);
  $relation->uid = $temp_uid;
  drupal_write_record('relation', $relation, $keys);
  if ($relation->is_new) {
    db_update('relation_revision')
      ->fields(array(
      'rid' => $relation->rid,
    ))
      ->condition('vid', $relation->vid)
      ->execute();
  }
  call_user_func("field_attach_{$op}", 'relation', $relation);
  module_invoke_all('entity_' . $op, $relation, 'relation');
  module_invoke('rules', 'invoke_event', 'relation_' . $op, $relation);
  relation_clear_related_entities_cache($endpoints);

  // Clear internal properties.
  unset($relation->is_new);
  unset($relation->original);
  return $relation->rid;
}