Drupal 7 module development and creation of a custom table (table) (hook_schema)

Source: Internet
Author: User
Tags drupal

For details about how to create a module, see Drupal 7 module development and establishment.

If you want to support Chinese characters, the file format must be saved as UTF-8, no BOM

------------------------------

To create a separate table for your module, you need to useHook_schema

Table Name: myform


Function my_first_module_schema () {$ schema ['myform'] = array ('description' => 'first Form ', 'fields' => array ('id' => array ('type' => 'serial', 'unsigned' => true, 'not null' => true, 'description' => 'auto-increment primary key'), 'title' => array ('type' => 'varchar ', 'length' => 255, 'Not null' => true, 'default' => '', 'description' => 'title ',), 'fullname' => array ('type' => 'varchar ', 'length' => 64, 'not null' => true, 'default' => '', 'description' => 'name',), 'email '=> array ('type' => 'varchar', 'length' => 255, 'Not null' => true, 'default' => '', 'description' => 'email ',), 'body' => array ('type' => 'text', 'not null' => false, 'SIZE' => 'Big ', 'serialize' => true, 'description' => 'message content',), 'timestamp' => array ('type' => 'int', 'not null' => true, 'default' => 0, 'description' => 'message time ',),), 'indexes' => array ('myform _ timestamp' => array ('timestamp'),), 'Primary key' => array ('id '),); return $ Schema ;}

For detailed usage of $ schema, refer to schema API

Description

String. Plain text format. Table description

Fields Field

Array. Describes the structure of a database table. Array ('fieldname' => Specification). For more information, see the "detail fields" section below.

Primary key

Array. Indicates one or more primary keywords.

'primary key' => array('id'),

Unique keys unique key

Array. ('Key name' => Specification)

Foreign keys foreign key

Array. ('Foreign key name' => Specification)

Specification Structure

Array (
'Table' => 'appearance name ',
'Columns' => array ('table field name' => 'external field name ')
)

  'foreign keys' => array(    'node_revision' => array(      'table' => 'node_revision',      'columns' => array('vid' => 'vid'),     ),    'node_author' => array(      'table' => 'users',      'columns' => array('uid' => 'uid'),     ),   ),

The above Code Description:

  • The foreign key names are node_revision and node_author.
  • Node_author associates the uid in this table with the uid in users.

Indexes Index

Array. ('Index name' => Specification ).

Specification structure: array ('field name 1', 'field name 2',...) can contain one or more

    'indexes' => array(      'myform_timestamp' => array('timestamp'),    ),

The above Code indicates that the timestamp is indexed and the index name is myform_timestamp.

Fields 

  • Description
    Field description

  • Type
    Field type. Common types include 'Char ', 'varchar', 'text', 'blob ', 'int', 'float', 'numeric', 'serial ', most types automatically correspond to a data type.
    Use Serial to specify an auto-increment field. MySQL automatically interprets it as int auto_increment.

  • Mysql_type,Pgsql_type,Sqlite_type, Etc.
    If you want to use an unofficial data type, you can specify a type for each database. In this case, you do not need to use the type parameter, but an error occurs when the database type is not specified. A writable solution is to use text as a backup in type.

    For example, type does not have the date and datetime types in MySQL. Here you can define 'mysql _ type' => 'date', 'mysql _ type' => 'datetime, an error occurs here. In this case, you need to write the int type first.

  • Serialize
    Boolean value, indicating whether the field will be stored as a serialized string

  • Size
    Data dimensions include tiny, small, medium, normal, and big. The default value is normal.
    For MySQL tables, see databaseschema_mysql: getfieldtypemap.

    MySQL size table
      Tiny Small Medium Normal Big
    Varchar       Varchar  
    Char       Char  
    Text Tinytext Tinytext Mediumtext Text Longtext
    Serial Tinyint Smallint Mediumint Int Bigint
    Int Tinyint Smallint Mediumint Int Bigint
    Float Float Float Float Float Double
    Numeric       Decimal  
    Blob       Blob Longblob


  • Not null
    If true, this field cannot be null ). The default value is false.

  • Default
    The default value of this field. Note the differences between '', '0' and 0.

  • Length
    This attribute is only applicable to Char, varchar, or text. Other types are ignored.

  • Unsigned
    Boolean value. It only indicates whether int, float, and numeric are signed. Other types are ignored. The default value is false.

  • Precision,Scale
    Only for the numeric type. Other types are ignored. The two values must be written.
    Precision is the total number of digits before and after the decimal point
    Scale is the number on the right of the decimal point.

  • Binary
    Boolean value. MySQL sorts the char, varchar, and text types in case-sensitive binary order. It is invalid for other databases that are case-sensitive by default.
    Example:
    If binary = true, A and A are sorted by ASCII code, and a is before.
    If binarry = false, A is sorted before.


For more data type definitions, refer to: Data Types Drupal converts different databases to corresponding SQL


MySQL common types

  • Auto-Increment(Auto_increment)

    'Id' => array ('type' => 'serial', 'unsigned' => true, 'not null' => true, 'description' => 'auto-increment primary key '),


  • Date type(Date, time, datetime, timestamp)

    'Timestamp' => array ('type' => 'int', 'not null' => true, 'default' => 0, 'description' => 'message time ',),




Reference: Drupal module development example: Basic Application of forms



Drupal 7 module development and creation of a custom table (table) (hook_schema)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.