0

I need to create a module to save data of term page visits.

<?php

function term_count_schema() {
  $schema['term_search'] = array(
  'description' => 'Stores timestamp range..',
'fields' => array(
  'tid' => array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'Taxonomy ID.',
   ),
  'vid' => array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'Vocabulary ID.',
    ),
  'timestamp' => array(
    'type' => 'int',
    'not null' => FALSE,
    'description' => 'UNIX timestamp.',
   ),
 ),
 'primary key' => array('tid'),

  );

  $schema['term_count'] = array(
'description' => 'Total views.',
'fields' => array(
  'tid' => array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'Taxonomay ID.',
  ),
  'vid' => array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'Vocabulary ID.',
  ),
  'totalviews' => array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'Total number of views which increments on every page load.',
  ),
),
'primary key' => array('tid'),
  );
  return $schema;
}

This is not creating tables term_search & term_count ind DB. Any thing wrong in my code.

1
    <?php
/**
 * @file
 * Contains install and update functions for Views.
 */

/**
 * Implements hook_install().
 */
function mid_components_install(){

}

/**
 * Implements hook_schema().
 *
 */
function mid_components_schema(){
    $schemas = array();
    $schemas['mid_components'] = array(
        'description' => 'The mid_components data.',
        'fields' => array(
            'cid' => array(
                'description' => 'Primary key',
                'type' => 'serial',
                'unsigned' => TRUE,
                'not null' => TRUE,
            ),
            'title' => array(
                'description' => 'Human readable name of the component.',
                'type' => 'varchar',
                'length' => 255,
                'not null' => TRUE,
            ),
            'machine_name' => array(
                'description' => 'Unique machine name of the component',
                'type' => 'varchar',
                'length' => 255,
                'not null' => TRUE,
            ),
            'description' => array(
                'description' => 'Description of the component.',
                'type' => 'varchar',
                'length' => 255,
                'not null' => FALSE,
            ),
            'options' => array(
                'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that component.',
                'type' => 'blob',
                'size' => 'big',
                'not null' => FALSE,
            ),
            'status' => array(
                'description' => 'Status of the component',
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
                'unsigned' => TRUE,
            ),
            'created' => array(
                'type' => 'int',
                'description' => 'The Unix timestamp when the item is created',
                'not null' => TRUE,
                'default' => 0,
            ),
            'changed' => array(
                'type' => 'int',
                'description' => 'The Unix timestamp when the item is modified.',
                'not null' => TRUE,
                'default' => 0,
            ),
        ),
        'primary key' => array('cid'),
        'unique key' => array('machine_name' => array('machine_name')),
    );
    return $schemas;
}

/**
 * Implements hook_uninstall().
 */
function mid_components_uninstall(){

}

Follow this method. And Make sure after doing any chnagings to your .install file, uninstall your module and reinstall it again.

  • Thanks Suagndh Khanna. It works for single table. I need one more table in DB. How i can create one more table within one hook_schema() – drup Jul 13 '15 at 10:25
  • You can have multiple schemas under single hook_schema. For better understanding try exploring Drupal contrib modules - .install file. – Sugandh Khanna Jul 13 '15 at 10:28

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.