如何在Drupal 7中使用.install文件创建表

时间:2015-07-13 12:17:30

标签: drupal drupal-7 schema

我是drupal的新手,在自定义模块上工作,这是我的.install文件代码,但是当我安装模块时它不在数据库中创建表。任何人都可以告诉我我错在哪里

<?php
    function make_application_schema()
    {
        $schema['make_master'] = array(

     'description' => 'Make master table',
     'fields' => array(
       'make_id' => array(
        'description' => 'make id primary key auto increment',
        'type' => 'serial',
        'not null' => TRUE,
       ),
      'make_name' => array(
        'description' => 'Make name',
        'type' => 'varchar',
        'length' => '100',
        'not null' => TRUE,
     ),
    'make_status' => array(
      'description' => 'check make status',
      'type' => 'int',
      'size' => 'tiny',
      'not null' => TRUE,
    ),
  ),
  'primary key' => array('make_id'),
);
return $schema;
}

  function make_application_install()
  {

  }

  function make_application_uninstall()
  {

  }

3 个答案:

答案 0 :(得分:1)

您的模块名称是什么?在安装并使用hook_schema时,您应该将您的函数命名为:

my_module.module

在my_module.install

$("input").on("keypress", function(e){
   var keypress;
   if(window.event) { // IE                    
      keypress= e.keyCode;
   } else if(e.which){ // Netscape/Firefox/Opera                   
      keypress= e.which;
   }
   if ( keypress == 13 ) {
      e.preventDefault();
   }
})

并且......之后,这应该是有效的:)

答案 1 :(得分:1)

为了安装您的数据库&#39; make_master&#39;你必须用你的模块名称

来调用drupal_install_schema
drupal_install_schema('make_application');

答案 2 :(得分:0)

根据您的问题,我认为您忘记致电drupal_install_schema功能。以下是make_application.install的更新make_application_installmake_application_uninstall

function make_application_install() {
// Use schema API to create database table.
   drupal_install_schema('make_master');
}


function make_application_uninstall() {
// Remove tables.
   drupal_uninstall_schema('make_master');
}


表格不会在模块启用时安装,它们只会在首次安装时安装。首先,禁用该模块,然后单击卸载&#39;选项卡,选择模块并卸载它(注意:如果您的模块没有hook_uninstall()函数,它将不会出现在此处 - 请确保您已添加此功能)。然后,单击列表选项卡,然后重新启用模块。这是首次安装,表格将安装。

或者使用devel模块,启用开发块,然后使用“重新安装模块”。块中的链接。

您可以参考此链接获取更多信息:https://www.drupal.org/node/811594