You are here

function entityreference_field_schema in Entity reference 8

Same name and namespace in other branches
  1. 7 entityreference.install \entityreference_field_schema()

Implements hook_field_schema().

File

./entityreference.install, line 12
Install, update and uninstall functions for the Entity reference module.

Code

function entityreference_field_schema($field) {
  $schema = array(
    'columns' => array(
      'target_id' => array(
        'description' => 'The id of the target entity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'target_id' => array(
        'target_id',
      ),
    ),
    'foreign keys' => array(),
  );

  // Create a foreign key to the target entity type base type.
  $entity_type = $field['settings']['target_type'];
  $entity_info = entity_get_info($entity_type);
  $base_table = $entity_info['base table'];
  $id_column = $entity_info['entity keys']['id'];
  $schema['foreign keys'][$base_table] = array(
    'table' => $base_table,
    'columns' => array(
      'target_id' => $id_column,
    ),
  );
  return $schema;
}