class DatabaseStorageController implements EntityStorageControllerInterface {
protected $entityCache;
protected $entityType;
protected $entityInfo;
protected $entityFieldInfo;
protected $hookLoadArguments;
protected $idKey;
protected $uuidKey;
protected $revisionKey;
protected $revisionTable;
protected $cache;
public function __construct($entityType) {
$this->entityType = $entityType;
$this->entityInfo = entity_get_info($entityType);
$this->entityCache = array();
$this->hookLoadArguments = array();
$this->idKey = $this->entityInfo['entity_keys']['id'];
if (!empty($this->entityInfo['entity_keys']['uuid'])) {
$this->uuidKey = $this->entityInfo['entity_keys']['uuid'];
}
else {
$this->uuidKey = FALSE;
}
if (!empty($this->entityInfo['entity_keys']['revision'])) {
$this->revisionKey = $this->entityInfo['entity_keys']['revision'];
$this->revisionTable = $this->entityInfo['revision_table'];
}
else {
$this->revisionKey = FALSE;
}
$this->cache = !empty($this->entityInfo['static_cache']);
}
public function resetCache(array $ids = NULL) {
if (isset($ids)) {
foreach ($ids as $id) {
unset($this->entityCache[$id]);
}
}
else {
$this->entityCache = array();
}
}
public function load(array $ids = NULL) {
$entities = array();
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
if ($this->cache && $ids) {
$entities += $this
->cacheGet($ids);
if ($passed_ids) {
$ids = array_keys(array_diff_key($passed_ids, $entities));
}
}
if ($ids === NULL || $ids) {
$query_result = $this
->buildQuery($ids)
->execute();
if (!empty($this->entityInfo['class'])) {
$query_result
->setFetchMode(PDO::FETCH_CLASS, $this->entityInfo['class'], array(
array(),
$this->entityType,
));
}
$queried_entities = $query_result
->fetchAllAssoc($this->idKey);
}
if (!empty($queried_entities)) {
$this
->attachLoad($queried_entities);
$entities += $queried_entities;
}
if ($this->cache) {
if (!empty($queried_entities)) {
$this
->cacheSet($queried_entities);
}
}
if ($passed_ids) {
$passed_ids = array_intersect_key($passed_ids, $entities);
foreach ($entities as $entity) {
$passed_ids[$entity
->id()] = $entity;
}
$entities = $passed_ids;
}
return $entities;
}
public function loadRevision($revision_id) {
$query_result = $this
->buildQuery(array(), $revision_id)
->execute();
if (!empty($this->entityInfo['class'])) {
$query_result
->setFetchMode(PDO::FETCH_CLASS, $this->entityInfo['class'], array(
array(),
$this->entityType,
));
}
$queried_entities = $query_result
->fetchAllAssoc($this->idKey);
if (!empty($queried_entities)) {
$this
->attachLoad($queried_entities, TRUE);
}
return reset($queried_entities);
}
public function deleteRevision($revision_id) {
if ($revision = $this
->loadRevision($revision_id)) {
if ($revision
->isDefaultRevision()) {
throw new EntityStorageException('Default revision can not be deleted');
}
db_delete($this->revisionTable)
->condition($this->revisionKey, $revision
->getRevisionId())
->execute();
$this
->invokeHook('revision_delete', $revision);
}
}
public function loadByProperties(array $values = array()) {
$entity_query = entity_query($this->entityType);
$this
->buildPropertyQuery($entity_query, $values);
$result = $entity_query
->execute();
return $result ? $this
->load($result) : array();
}
protected function buildPropertyQuery(QueryInterface $entity_query, array $values) {
foreach ($values as $name => $value) {
$entity_query
->condition($name, $value);
}
}
protected function buildQuery($ids, $revision_id = FALSE) {
$query = db_select($this->entityInfo['base_table'], 'base');
$query
->addTag($this->entityType . '_load_multiple');
if ($revision_id) {
$query
->join($this->revisionTable, 'revision', "revision.{$this->idKey} = base.{$this->idKey} AND revision.{$this->revisionKey} = :revisionId", array(
':revisionId' => $revision_id,
));
}
elseif ($this->revisionKey) {
$query
->join($this->revisionTable, 'revision', "revision.{$this->revisionKey} = base.{$this->revisionKey}");
}
$entity_fields = $this->entityInfo['schema_fields_sql']['base_table'];
if ($this->revisionKey) {
$entity_revision_fields = drupal_map_assoc($this->entityInfo['schema_fields_sql']['revision_table']);
unset($entity_revision_fields[$this->idKey]);
$entity_field_keys = array_flip($entity_fields);
foreach ($entity_revision_fields as $key => $name) {
if (isset($entity_field_keys[$name])) {
unset($entity_fields[$entity_field_keys[$name]]);
}
}
$query
->fields('revision', $entity_revision_fields);
$query
->addExpression('base.' . $this->revisionKey . ' = revision.' . $this->revisionKey, 'isDefaultRevision');
}
$query
->fields('base', $entity_fields);
if ($ids) {
$query
->condition("base.{$this->idKey}", $ids, 'IN');
}
return $query;
}
protected function attachLoad(&$queried_entities, $load_revision = FALSE) {
if ($this->entityInfo['fieldable']) {
if ($load_revision) {
field_attach_load_revision($this->entityType, $queried_entities);
}
else {
field_attach_load($this->entityType, $queried_entities);
}
}
foreach (module_implements('entity_load') as $module) {
$function = $module . '_entity_load';
$function($queried_entities, $this->entityType);
}
$args = array_merge(array(
$queried_entities,
), $this->hookLoadArguments);
foreach (module_implements($this->entityType . '_load') as $module) {
call_user_func_array($module . '_' . $this->entityType . '_load', $args);
}
}
protected function cacheGet($ids) {
$entities = array();
if (!empty($this->entityCache)) {
$entities += array_intersect_key($this->entityCache, array_flip($ids));
}
return $entities;
}
protected function cacheSet($entities) {
$this->entityCache += $entities;
}
public function create(array $values) {
$class = $this->entityInfo['class'];
$entity = new $class($values, $this->entityType);
if ($this->uuidKey && !isset($entity->{$this->uuidKey})) {
$uuid = new Uuid();
$entity->{$this->uuidKey} = $uuid
->generate();
}
return $entity;
}
public function delete(array $entities) {
if (!$entities) {
return;
}
$transaction = db_transaction();
try {
$this
->preDelete($entities);
foreach ($entities as $id => $entity) {
$this
->invokeHook('predelete', $entity);
}
$ids = array_keys($entities);
db_delete($this->entityInfo['base_table'])
->condition($this->idKey, $ids, 'IN')
->execute();
if ($this->revisionKey) {
db_delete($this->revisionTable)
->condition($this->idKey, $ids, 'IN')
->execute();
}
$this
->resetCache($ids);
$this
->postDelete($entities);
foreach ($entities as $id => $entity) {
$this
->invokeHook('delete', $entity);
}
db_ignore_slave();
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage, $e->getCode, $e);
}
}
public function save(EntityInterface $entity) {
$transaction = db_transaction();
try {
if (!$entity
->isNew() && !isset($entity->original)) {
$entity->original = entity_load_unchanged($this->entityType, $entity
->id());
}
$this
->preSave($entity);
$this
->invokeHook('presave', $entity);
if (!$entity
->isNew()) {
if ($entity
->isDefaultRevision()) {
$return = drupal_write_record($this->entityInfo['base_table'], $entity, $this->idKey);
}
else {
$return = FALSE;
}
if ($this->revisionKey) {
$this
->saveRevision($entity);
}
$this
->resetCache(array(
$entity
->id(),
));
$this
->postSave($entity, TRUE);
$this
->invokeHook('update', $entity);
}
else {
$return = drupal_write_record($this->entityInfo['base_table'], $entity);
if ($this->revisionKey) {
$this
->saveRevision($entity);
}
$this
->resetCache(array());
$entity
->enforceIsNew(FALSE);
$this
->postSave($entity, FALSE);
$this
->invokeHook('insert', $entity);
}
db_ignore_slave();
unset($entity->original);
return $return;
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e
->getMessage(), $e
->getCode(), $e);
}
}
protected function saveRevision(EntityInterface $entity) {
$record = (array) $entity;
if ($entity
->isNewRevision() && $record[$this->revisionKey]) {
$record[$this->revisionKey] = NULL;
}
$record = (object) $record;
$this
->preSaveRevision($record, $entity);
$record = (array) $record;
if ($entity
->isNewRevision()) {
drupal_write_record($this->revisionTable, $record);
if ($entity
->isDefaultRevision()) {
db_update($this->entityInfo['base_table'])
->fields(array(
$this->revisionKey => $record[$this->revisionKey],
))
->condition($this->idKey, $entity
->id())
->execute();
}
$entity
->setNewRevision(FALSE);
}
else {
drupal_write_record($this->revisionTable, $record, $this->revisionKey);
}
$entity->{$this->revisionKey} = $record[$this->revisionKey];
}
protected function preSave(EntityInterface $entity) {
}
protected function postSave(EntityInterface $entity, $update) {
}
protected function preDelete($entities) {
}
protected function postDelete($entities) {
}
protected function preSaveRevision(\stdClass $record, EntityInterface $entity) {
}
protected function invokeHook($hook, EntityInterface $entity) {
$function = 'field_attach_' . $hook;
if ($function == 'field_attach_revision_delete') {
$function = 'field_attach_delete_revision';
}
if (!empty($this->entityInfo['fieldable']) && function_exists($function)) {
$function($this->entityType, $entity);
}
module_invoke_all($this->entityType . '_' . $hook, $entity);
module_invoke_all('entity_' . $hook, $entity, $this->entityType);
}
public function getFieldDefinitions(array $constraints) {
if (!isset($this->entityFieldInfo)) {
$this->entityFieldInfo = array(
'definitions' => $this
->baseFieldDefinitions(),
'optional' => array(),
'bundle map' => array(),
);
$result = module_invoke_all($this->entityType . '_property_info');
$this->entityFieldInfo = array_merge_recursive($this->entityFieldInfo, $result);
$result = module_invoke_all('entity_field_info', $this->entityType);
$this->entityFieldInfo = array_merge_recursive($this->entityFieldInfo, $result);
$hooks = array(
'entity_field_info',
$this->entityType . '_property_info',
);
drupal_alter($hooks, $this->entityFieldInfo, $this->entityType);
foreach ($this->entityFieldInfo['definitions'] as &$definition) {
$definition['list'] = TRUE;
}
foreach ($this->entityFieldInfo['optional'] as &$definition) {
$definition['list'] = TRUE;
}
}
$definitions = $this->entityFieldInfo['definitions'];
if (!empty($constraints['bundle']) && isset($this->entityFieldInfo['bundle map'][$constraints['bundle']])) {
$definitions += array_intersect_key($this->entityFieldInfo['optional'], array_flip($this->entityFieldInfo['bundle map'][$constraints['bundle']]));
}
return $definitions;
}
public function baseFieldDefinitions() {
return array();
}
public function getQueryServiceName() {
return 'entity.query.field_sql_storage';
}
}