create install script for drupal module and create tables

Drupal is most popular CMS in the world for creating the web application. For custom purpose we need to create the drupal modules. We need the custom tables for creating the drupal.

create install script for drupal module and create tables

create install script for drupal module and create tables

create install script for drupal module and create tables

In this article I will tell how you can create the sample drupal module with custom two table. When you activate the drupal module then tables which are defined in the module will be get installed. When you unactive the drupal module tables will get deleted from database.

For creating the drupal module three files are necessary and that as follow:

1. yourmodule.info (you can use your module name

2. yourmodule.install

3. yourmodule.module

First .info file is important and in that file we will put module related information. mymodule.info file has following content.


; $Id$
name = My module
description = This module is to test the install feature
core = 6.x

For creating custom tables you can use following code in mymodule.install file.


<?php
function mymodule_schema() {
 $schema['mymodule_test'] = array(
 'description' =--> t('The base table for saved articles.'),
 'fields' => array(
 'id' => array(
 'description' => t('The primary identifier'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE),
 'uid' => array(
 'description' => t('The user identifier.'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE,
 'default' => 0),
 'name' => array(
 'description' => t('The name.'),
 'type' => 'varchar',
 'length' => '100',
 'not null' => TRUE,
 'not null' => TRUE),
 ),
 'primary key' => array('id'),
 );

 $schema['mymodule_test1'] = array(
 'description' => t('The base table for saved articles.'),
 'fields' => array(
 'id' => array(
 'description' => t('The primary identifier'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE),
 'uid1' => array(
 'description' => t('The user identifier.'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE,
 'default' => 0),
 'name' => array(
 'description' => t('The name.'),
 'type' => 'varchar',
 'length' => '100',
 'not null' => TRUE,
 'not null' => TRUE),
 ),
 'primary key' => array('id'),
 );

 return $schema;
}

function mymodule_install() {
 // Create my tables.
 drupal_install_schema('mymodule');
}

function mymodule_uninstall() {
 // Drop my tables.
 drupal_uninstall_schema('mymodule');
}
?>

.Module file is most important. In this file you can put the code which for you written the module. Now in this file Now I am giving you the sample menu code. mymodule.module file has following content.


<?php
function mymodule_menu() {

 $items['mymodule'] = array(
 'title' =--> 'mymodule View',
 'page callback' => 'mymodule_view',
 'access arguments' => array('access content'),
 );

 return $items;
}

function mymodule_view()
{
 return 'test';
}

?>

If you have some more doubts then please content me and comment on this article.

If you want more drupal tutorials than visit here

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *