Drupal 6: Define a numeric field type in the database schema

1 Comment

Often it is useful to define numeric fields in the database for information such as pricing or measurement.

In Drupal, the way to define a numeric type is to also declare the "precision" option.

$schema['products'] = array(
  'description' => t('Stores product data.'),
  'fields' => array(
    'prodid' => array('type' => 'int', 'not null' => TRUE),
    'manufacturer' => array('type' => 'text'),
    'product_name' => array('type' => 'text'),
    'description' => array('type' => 'text'),
    'price' => array('type' => 'numeric', 'precision' => 2),
  ),
  'primary key' => array(prodid'),
);

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog