首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请解释Drupal schema和drupal_write_record

请解释Drupal schema和drupal_write_record
EN

Stack Overflow用户
提问于 2010-04-21 21:45:00
回答 3查看 2.1K关注 0票数 2

1)在第一次安装、启用模块时,填充新数据库表的最佳位置是什么?我需要从外部源获取一些数据,并且希望在用户安装/启用我的自定义模块时透明地执行此操作。

我在{mymodule}_schema()中创建模式,在hook_install中执行drupal_install_schema({tablename});。然后,我尝试使用drupal_write_record在hook_enable中填充表。

我确认表已经创建,在执行hook_enable时没有出现错误,但是当我查询新表时,没有得到任何行--它是空的。

下面是我尝试过的代码的一个变体:

代码语言:javascript
复制
/**
* Implementation of hook_schema()
*/
function ncbi_subsites_schema() {
    // we know it's MYSQL, so no need to check
    $schema['ncbi_subsites_sites'] = array(
        'description' => 'The base table for subsites',
        'fields' => array(
            'site_id' => array(
                'description' => 'Primary id for site',
                'type' => 'serial',
                'unsigned' => TRUE,
                'not null' => TRUE,
            ), // end site_id
            'title' => array(
                'description' => 'The title of the subsite',
                'type' => 'varchar',
                'length' => 255,
                'not null' => TRUE,
                'default' => '',
            ), //end title field
            'url' => array(
                'description' => 'The URL of the subsite in Production',
                'type' => 'varchar',
                'length' => 255,
                'default' => '',
            ), //end url field
        ), //end fields
        'unique keys' => array(
            'site_id'=> array('site_id'),
            'title' => array('title'),
        ), //end unique keys
        'primary_key' => array('site_id'),
    ); // end schema

    return $schema;
}

下面是hook_install:

代码语言:javascript
复制
function ncbi_subsites_install() {
    drupal_install_schema('ncbi_subsites');
}

下面是hook_enable:

代码语言:javascript
复制
function ncbi_subsites_enable() {
    drupal_get_schema('ncbi_subsites_site');

    // my helper function to get data for table (not shown)
    $subsites = ncbi_subsites_get_subsites(); 
    foreach( $subsites as $name=>$attrs ) {
        $record = new stdClass();
        $record->title = $name;
        $record->url = $attrs['homepage'];
        drupal_write_record( 'ncbi_subsites_sites', $record );
    }
}

有人能告诉我我错过了什么吗?

EN

回答 3

Stack Overflow用户

发布于 2010-04-22 02:38:09

如果.install文件中没有ncbi_subsites_get_subsites(),则需要在模块中包含它所在的文件。否则,它将不返回任何内容,在这种情况下,尝试转储$subsites并退出。

票数 1
EN

Stack Overflow用户

发布于 2010-04-29 21:22:59

我认为答案是drupal_write_record不是用来安装或启用钩子的。我认为在启用或安装时,您必须编写SQL。这就是我在阅读一些帖子时得到的印象,这些帖子提到模式在这些钩子中不可用。

票数 1
EN

Stack Overflow用户

发布于 2010-08-18 03:16:27

首先(假设Drupal6),不能从hook_install()调用drupal_write_record(),因为Drupal找不到从模块定义的数据库模式,该模块仍将被安装和启用。

相反,您需要使用db_query()函数。(评论中提到了一种通过将缺省数据提供给 serialized来包含缺省数据的方法,但我还没有找到有关这方面的文档。)

但是,如果您使用的是Drupal7(开发版本),那么您需要查看db_insert()函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2683422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档