-
Notifications
You must be signed in to change notification settings - Fork 0
/
CiviCRMEntityController.inc
83 lines (74 loc) · 2.52 KB
/
CiviCRMEntityController.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
class CiviCRMEntityController extends EntityAPIController implements EntityAPIControllerInterface {
/**
* Implements EntityAPIControllerInterface.
*
* @param $transaction
* Optionally a DatabaseTransaction object to use. Allows overrides to pass
* in their transaction object.
*/
public function delete($ids, DatabaseTransaction $transaction = NULL) {
$entities = $ids ? $this->load($ids) : FALSE;
if (!$entities) {
// Do nothing, in case invalid or no ids have been passed.
return;
}
// This transaction causes troubles on MySQL, see
// http://drupal.org/node/1007830. So we deactivate this by default until
// is shipped in a point release.
// $transaction = isset($transaction) ? $transaction : db_transaction();
try {
$ids = array_keys($entities);
// Reset the cache as soon as the changes have been applied.
$this->resetCache($ids);
foreach ($entities as $id => $entity) {
$this->invoke('delete', $entity);
}
// Ignore slave server temporarily.
db_ignore_slave();
}
catch (Exception $e) {
if (isset($transaction)) {
$transaction->rollback();
}
watchdog_exception($this->entityType, $e);
throw $e;
}
}
/**
* Implements EntityAPIControllerInterface.
*
* @param $transaction
* Optionally a DatabaseTransaction object to use. Allows overrides to pass
* in their transaction object.
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
$transaction = isset($transaction) ? $transaction : db_transaction();
try {
// Load the stored entity, if any.
if (!empty($entity->{$this->idKey}) && !isset($entity->original)) {
// In order to properly work in case of name changes, load the original
// entity using the id key if it is available.
$entity->original = entity_load_unchanged($this->entityType, $entity->{$this->idKey});
}
$this->invoke('presave', $entity);
if (!empty($entity->{$this->idKey}) && empty($entity->is_new)) {
$this->resetCache(array($entity->{$this->idKey}));
$this->invoke('update', $entity);
}
else {
$this->invoke('insert', $entity);
}
// Ignore slave server temporarily.
db_ignore_slave();
unset($entity->is_new);
unset($entity->original);
return TRUE;
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception($this->entityType, $e);
throw $e;
}
}
}