You are here

function eck_schema in Entity Construction Kit (ECK) 7

Same name and namespace in other branches
  1. 7.3 eck.install \eck_schema()
  2. 7.2 eck.install \eck_schema()

Implements hook_schema().

Create the database table that will store the entities information. All that we need for each entity is a name and a label

File

./eck.install, line 29
This install file creates a table for the this module to store information about entities. Well, the only information that it needs to store is a name and a label for each entity created, the rest of the information is generated by the functions.

Code

function eck_schema() {
  module_load_include('inc', 'eck', 'eck.entity_type');
  $schema = array();

  // Schema for the eck table.
  $schema['eck_entity_type'] = array(
    'description' => "The base table for entity type information",
    'fields' => array(
      'name' => array(
        'description' => "The machine name of the entity",
        'type' => 'varchar',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => "The entity's Label",
        'type' => 'varchar',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'properties' => array(
        'type' => 'text',
        'not null' => TRUE,
        'serialize' => TRUE,
        'initial' => serialize(array()),
        'description' => 'A serialized list of properties attached to this entity.',
      ),
      'custom_properties' => array(
        'type' => 'text',
        'not null' => TRUE,
        'serialize' => TRUE,
        'initial' => serialize(array()),
        'description' => 'A serialized list of custom properties attached to this entity.',
      ),
    ),
    'primary key' => array(
      'name',
    ),
    'indexes' => array(),
  );
  $schema['eck_bundle'] = array(
    'description' => "The base table for bundle information",
    'fields' => array(
      /*Really the identifier should be some sort of combination between'
       * the entity type the bundle belongs too, and the bundles name
       * but since I don't know db stuff to that extent, i will leave this
       * useless id hanging around */
      'id' => array(
        'description' => "The primary identifier for a bundle",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'entity_type' => array(
        'description' => "The entity type this bundle belongs to",
        'type' => 'varchar',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => "The bundle's name",
        'type' => 'varchar',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => "A human readable name for the bundle (not that the type is not human readable)",
        'type' => 'varchar',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'indexes' => array(
      'entity_type_bundle' => array(
        'entity_type',
        'name',
      ),
    ),
  );

  // At the installation stage, eck has not been created, so what is this for?
  // The schema is not only used during installation, it is used at many other
  // stages and by many other processes withing drupal. Because we are creating
  // tables dynamically with eck, we need to expose those schemas through this
  // function, that is why we are doing this.
  if (db_table_exists('eck_entity_type')) {

    // When something requests an entity's info, the hook_schema is called to
    // get the information about the entity's table, so we need to provide that
    // information in the hook.
    // Get all the entity types that have been create (all the rows in eck_entity_type table).
    foreach (eck__entity_type__load() as $entity_type) {

      // The function eck__entity_type__schema returns a schema for that entity type
      // given and entity_type object.
      $schema = array_merge($schema, array(
        "eck_{$entity_type->name}" => eck__entity_type__schema($entity_type),
      ));
    }
  }
  return $schema;
}