1

It is my first time that I actually want to make use of Entity API module. I looked at different modules like profile2 which has implemented their module with Entity API. I do not exactly understand what "data" column does in both for example, profile and profile_type base tables for profile2 module? As far as I see, it's a common habit in modules developed by Entity API.

/**
 * Implements hook_schema().
 */
function profile2_schema() {
  $schema['profile'] = array(
    'description' => 'Stores profile items.',
    'fields' => array(
      'pid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique profile item ID.',
      ),
      'type' => array(
        'description' => 'The {profile_type}.type of this profile.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => NULL,
        'description' => "The {users}.uid of the associated user.",
      ),
      'label' => array(
        'description' => 'A human-readable label for this profile.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the profile was created.',
        'type' => 'int',
        'not null' => FALSE,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the profile was most recently saved.',
        'type' => 'int',
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'uid' => array('uid'),
    ),
    'foreign keys' => array(
      'uid' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'type' => array(
        'table' => 'profile_type',
        'columns' => array('type' => 'type'),
      ),
    ),
    'primary key' => array('pid'),
  );

  $schema['profile_type'] = array(
    'description' => 'Stores information about all defined profile types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique profile type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this profile type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this profile type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The weight of this profile type in relation to others.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of additional data related to this profile type.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'type' => array('type'),
    ),
  );
  return $schema;
}
1
  • The best place to learn Entities is a DrupalCommerce module..
    – xurshid29
    Dec 27, 2013 at 7:06

1 Answer 1

0

The data column is not required and is not exclusive to entity modules. It is primarily used to add metadata to records by the module itself or by other modules. The data stored here is usually in the form of a serialized array, as such can contain a number of different small bits of info (e.g. IP of the creator, size of the file). This can be particularly helpful for modules to attach small bits of info related to entities or records without having the need to create a table or add new columns.

There is, however, a downside to implementing a data column - it can be overused / abused. Due to convenience or laziness, important info are sometimes just saved in the data column. This causes some problems like:

  1. Migration - as in the case of D6/Ubercart where important info such as the attribute of the ordered product are saved in the data column of the orders table making it hard to migrate to D7/Commerce.
  2. Makes queries complex - since the data column is usually a blob or large text data type, the only option you have to query this column is to use LIKE or REGEXP.

In summary, the data column is used to store metadata, but it can also be used for business logic (although I strongly advise against using the data column in this way).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.