You are here

final public function PartyDefaultDataSet::save in Party 7

Same name and namespace in other branches
  1. 8.2 includes/party.data.inc \PartyDefaultDataSet::save()

Save the attached entities information

This method cannot be overloaded and any extensions of this class can make use of PartyDefaultDataSet::preSave() and PartyDefaultDataSet::postSave() to perform any additional logic required.

As saving entities could cause further calls to PartyDefaultDataSet::save(), the $has_lock variable stores whether this call to PartyDefaultDataSet::save() is a top level save. Only the top level save will update the party_attached_entity table and store changes to $party->label to prevent PDO issues and conflicting data. Nested calls shouldn't make any unnecessary database writes, both for performance and to prevent data conflicts. This information is handed on to the PartyDefaultDataSet::preSave() and PartyDefaultDataSet::postSave() for overloading classes to continue this behavior. An example of why this is necessary can be found in the issue at http://drupal.org/node/1907744.

@todo Remove the need to save entity type and bundle, as it's easy to sniff our from the data set.

Parameters

bool $save_entities: Whether or not to save entities as we go through them. If an entity is unsaved, we will always save it, as otherwise we can't save the deltas.

Return value

$this

See also

PartyDefaultDataSet::preSave()

PartyDefaultDataSet::postSave()

File

includes/party.data.inc, line 822
Provides the default class for managing party - Attached entity relationships.

Class

PartyDefaultDataSet
Class PartyDefaultDataSet

Code

public final function save($save_entities = FALSE) {
  $has_lock = FALSE;
  if (!$this->saving) {

    // Lock it down to prevent concurrency.
    $this->saving = $has_lock = TRUE;
  }

  // Fire any pre attach logic
  $this
    ->preSave($save_entities, $has_lock);

  // Save any entities that need saving.
  foreach ($this->entities as $delta => &$entity) {
    if ($save_entities || isset($entity->is_new)) {

      // Save our entities as we go
      $this
        ->saveEntity($delta);
    }
  }
  if (!empty($has_lock)) {
    $this
      ->clear();

    // Insert our entities
    $query = db_insert('party_attached_entity');
    $query
      ->fields(array(
      'pid',
      'eid',
      'delta',
      'data_set',
      'entity_type',
      'entity_bundle',
    ));
    foreach ($this->entities as $delta => &$entity) {

      // Add our record
      $query
        ->values(array(
        'pid' => $this->party->pid,
        'eid' => $entity->{$this
          ->getDataInfo('id key')},
        'delta' => $delta,
        'data_set' => $this->data_set,
        'entity_type' => $this
          ->getDataInfo('entity type'),
        'entity_bundle' => $this
          ->getDataInfo('entity bundle'),
      ));
    }
    $query
      ->execute();

    // Release our lock.
    $this->saving = FALSE;
  }

  // Fire any post attach logic
  $this
    ->postSave($save_entities, $has_lock);
  if (!empty($has_lock)) {
    $this
      ->invoke('save');
  }

  // Update the label and primary fields whenever attached entities are
  // changed. This should only trigger a store if we are a locking save.
  $controller = entity_get_controller('party');
  $controller
    ->setPrimaryFields($this->party, $has_lock);
  return $this;
}