0

I've written a module that uses entities functionality. It has the following table scheme in .install file:

$schema['transaction'] = array(
    //fields
    'fields' => array(
        'id' => array(
            'type' => 'serial',
            'unsigned' => TRUE,
            'not null' => TRUE),
        'name' => array(
            'type' => 'varchar',
            'length' => 50,
            'not null' => TRUE,
            ),
        'random_id' => array(
            'type' => 'varchar',
            'length' => 11,
            'not null' => TRUE,
            ),
        ),
    //additional properties
    'primary key' => array('id'),
    'unique keys' => array(
        'random_id' => array('random_id'),
        ),
    );

Entity description:

function transaction_entity_info() {
  //song entity
  $entity_info['transaction'] = array(
    'label' => t('Transaction'),
    'label callback' => 'transaction_label_callback',
    'base table' => 'transaction',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
    ),
    'uri callback' => 'transaction_class_uri',
    'static cache' => TRUE,
    'admin ui' => array(
      'path' => 'admin/content/transaction',
      'controller class' => 'transactionUIController',
      'file' => 'includes/transaction.admin.inc',
    ),
    'module' => 'transaction',
    'access callback' => 'transaction_access_callback',
    'bundles' => array(
      'transaction' => array(
        'label' => 'Transaction',
        'admin' => array(
          'path' => 'admin/structure/transaction/manage',
          'access arguments' => array('administer transaction'),
        ),
      ),
    ),
    'entity class' => 'Transaction',
    'controller class' => 'transactionController',
    'views controller class' => 'EntityDefaultViewsController',
  );

  return $entity_info;
}

Now I need to add a new property into the table:

    'output' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),

I can use fields, and add it through the admin panel, but I want to learn, how to upgrade the tables directly.

How can tell Drupal to reload the table scheme for this module, add attribute into the table and use it when loading the entity?

3
  • You are not showing a table schema used for an entity; you are showing a schema for a normal database table.
    – apaderno
    Jun 11 '15 at 13:28
  • @kiamlaluno I added the entity description.
    – user4035
    Jun 11 '15 at 13:36
  • 1
    Point being that there's no difference - just add the extra definition to the schema and make changes like you would with any table in an update hook. If you have any entity module integration that will probably need to be updated too
    – Clive
    Jun 11 '15 at 14:21
1

@Clive is correct (in the comments). You'll just want to add the new definition to the schema hook and then create an update script in the .install file (hook_update_N) to add in that definition for sites/installations that have already created your new entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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