You are here

final public function PartyDataBase::save in Party 8.2

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.

@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

lib/Drupal/party/Plugin/PartyDataBase.php, line 590
Contains \Drupal\party\Plugin\PartyDataBase.

Class

PartyDataBase
Class PartyDataBase

Namespace

Drupal\party\Plugin

Code

public final function save($save_entities = FALSE) {

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

  // Clear out our old bits
  $query = db_delete('party_attached_entity');
  $query
    ->condition('pid', $this->party
    ->id(), '=');
  $query
    ->condition('data_set_name', $this->dataName);
  $query
    ->execute();

  // Insert our entities
  $query = db_insert('party_attached_entity');
  $query
    ->fields(array(
    'party_id',
    'entity_id',
    'delta',
    'data_set_name',
    'entity_type',
    'entity_bundle',
  ));
  foreach ($this->entities as $delta => &$entity) {
    if ($save_entities || $entity
      ->isNew()) {

      // Save our entities as we go
      $this
        ->saveEntity($delta);
    }

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

  // Update the label whenever attached entities are changed.
  drupal_container()
    ->get('plugin.manager.entity')
    ->getStorageController('party')
    ->setLabel($this->party);

  // Fire any post attach logic
  $this
    ->postSave($save_entities);
  return $this;
}