You are here

eck.install in Entity Construction Kit (ECK) 7

Same filename and directory in other branches
  1. 8 eck.install
  2. 7.3 eck.install
  3. 7.2 eck.install

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.

eck__entity_info efacoty__entity_schema eck__entity_menu

in eck.module

File

eck.install
View source
<?php

/**
 * @file
 * 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.
 *
 * eck__entity_info
 * efacoty__entity_schema
 * eck__entity_menu
 *
 * in eck.module
 */

/**
 * TODO: Currently when the module is uninstalled, the tables of Entities that
 * were left behind are still around, can we warn the user when they are trying
 * to uninstall about the left over entities that are being managed?
 */

/**
 * 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
 */
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;
}

/**
 * Update 7000
 */
function eck_update_7000() {

  // To implement bundles (types), I have decided to have one table that will
  // hold all of the types information. So for this update we need to create the
  // table, and add all of the current entities with their types to the table,
  // so we can unify the implementations of bundles in code.
  $schema = array(
    'description' => "The base table for entities types information",
    'fields' => array(
      'id' => array(
        'description' => "The primary identifier for a(n) entity type",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'entity' => array(
        'description' => "The entity this type belongs to",
        'type' => 'varchar',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => "The type (bundle) name that will be used",
        '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' => array(
        'entity',
        'type',
      ),
    ),
  );
  db_create_table('eck_types', $schema);

  // Now we add a type (bundle) to the table for each entity in the eck table.
  $results = db_select('eck', 't')
    ->fields('t')
    ->execute();
  foreach ($results as $record) {
    $nid = db_insert('eck_types')
      ->fields(array(
      'entity' => $record->name,
      'type' => $record->name,
      'label' => $record->label,
    ))
      ->execute();

    // Also we want to add a field to all the enity tables for the type, and
    // populated with the current type.
    db_add_field("eck_{$record->name}", 'type', array(
      'description' => 'The type of the entity',
      'type' => 'varchar',
      'default' => '',
      'length' => 255,
      'not null' => TRUE,
      'initial' => $record->name,
    ));
  }
}

/**
 * Update 7001
 */
function eck_update_7001() {

  // For importing and exporting things it is always good to have a uuid, so we
  // will add that colunm to the current entity types.
  // Now we add a type (bundle) to the table for each entity in the eck table.
  $results = db_select('eck', 't')
    ->fields('t')
    ->execute();
  foreach ($results as $record) {

    // Also we want to add a field to all the enity tables for the type, and
    // populated with the current type.
    db_add_field("eck_{$record->name}", 'uuid', array(
      'type' => 'char',
      'length' => 36,
      'not null' => TRUE,
      'default' => '',
      'description' => 'The Universally Unique Identifier.',
    ));
  }
}
function eck_update_7002() {

  // The more I think about it, the more it makes sense to replicate the
  // "status" property from nodes, this is just a flag that lets the system know
  // whether a piece of data should be seen by the public or not. Instead of a
  // boolean, I think that a int instead of boolean could be more useful, that
  // way more complicated workflows can be implemented. For example if a piece
  // of data needs to be reviewed before showing it to the public, and int can
  // campture those different states: 0- don't show 1 - show 3 - needs review 4
  // - revised by editor, etc. Also instead of status, I think that "state" is a
  // more appropiate name.
  $results = db_select('eck', 't')
    ->fields('t')
    ->execute();
  foreach ($results as $record) {
    db_add_field("eck_{$record->name}", 'state', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
      'description' => 'The state of the entity',
    ));
  }
}

/**
 * Adds the properties column to the eck table.
 */
function eck_update_7003() {
  db_add_field('eck', 'properties', array(
    'type' => 'text',
    'not null' => TRUE,
    'serialize' => TRUE,
    'initial' => serialize(array(
      'uuid' => 1,
      'uid' => 1,
      'created' => 1,
      'changed' => 1,
      'state' => 1,
    )),
    'description' => 'A serialized list of additional properties attached to this entity.',
  ));
}

/**
 * Adds the custom properties column to the eck table.
 */
function eck_update_7004() {
  db_add_field('eck', 'custom_properties', array(
    'type' => 'text',
    'not null' => TRUE,
    'serialize' => TRUE,
    'initial' => serialize(array()),
    'description' => 'A serialized list of custom properties attached to this entity.',
  ));
}
function eck_update_7005() {

  //change the name of the eck table to eck_entity_type
  db_rename_table('eck', 'eck_entity_type');

  //change the name of the eck_types table to eck_bundle
  db_rename_table('eck_types', 'eck_bundle');

  //remove the id key from eck_entity_type
  db_drop_field('eck_entity_type', 'id');

  //create the index with the right field names
  db_add_primary_key('eck_entity_type', array(
    'name',
  ));

  //drop the eck_bundle index entity_type
  db_drop_index('eck_bundle', 'entity_type');

  //change eck_bundle entity field to entity_type
  db_change_field('eck_bundle', 'entity', 'entity_type', array(
    'description' => "The entity type this bundle belongs to",
    'type' => 'varchar',
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
  ));

  //change eck_bundle type field to name
  db_change_field('eck_bundle', 'type', 'name', array(
    'description' => "The bundle's name",
    'type' => 'varchar',
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
  ));

  //create the index with the right field names
  db_add_index('eck_bundle', 'entity_type_bundle_name', array(
    'entity_type',
    'name',
  ));
}

Functions

Namesort descending Description
eck_schema Implements hook_schema().
eck_update_7000 Update 7000
eck_update_7001 Update 7001
eck_update_7002
eck_update_7003 Adds the properties column to the eck table.
eck_update_7004 Adds the custom properties column to the eck table.
eck_update_7005