final public function PartyDefaultDataSet::save in Party 8.2
Same name and namespace in other branches
- 7 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()
2 calls to PartyDefaultDataSet::save()
- PartyDefaultDataSet::hook_party_delete in includes/
party.data.inc - Acts when a party is deleted.
- PartyUserDataSet::hook_party_delete in modules/
party_user/ includes/ party_user.data.inc - Act on party delete.
File
- includes/
party.data.inc, line 654 - 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)) {
// Clear out our old bits
$query = db_delete('party_attached_entity');
$query
->condition('pid', $this->party->pid, '=');
$query
->condition('data_set', $this->data_set);
$query
->execute();
// 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);
// Update the label whenever attached entities are changed. This should
// only trigger a store if we are a locking save.
entity_get_controller('party')
->setLabel($this->party, $has_lock);
return $this;
}