views:

333

answers:

3

Hi. A few questions.

1) Where is the best place to populate a new database table when a module is first installed, enabled? I need to go and get some data from an external source and want to do it transparently when the user installs/enables my custom module.

I create the schema in {mymodule}_schema(), do drupal_install_schema({tablename}); in hook_install. Then I try to populate the table in hook_enable using drupal_write_record.

I confirmed the table was created, I get no errors when hook_enable executes, but when I query the new table, I get no rows back--it's empty.

Here's one variation of the code I've tried:

/**
* 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;
}

Here's hook_install:

function ncbi_subsites_install() {
    drupal_install_schema('ncbi_subsites');
}

Here's hook_enable:

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 );
    }
}

Can someone tell me what I'm missing?

+1  A: 

If ncbi_subsites_get_subsites() is not in the .install file, you need to include whatever file its in with your module. Otherwise, it's returning nothing, in which case try dumping $subsites and exiting.

Kevin
I am including that function via an include
Aaron
What happens when you dump $subsites, then? What does that function do?
Kevin
+1  A: 

I think the answer is that drupal_write_record is not meant for install or enable hooks. I think when enabling or installing, you have to write SQL. That is the impression I am getting from reading some posts that mention that the schema is not available in these hooks.

Aaron
+1  A: 

First of all (assuming Drupal 6), drupal_write_record() cannot be called from hook_install() because Drupal would not find the database schema defined from the module, which is still going to be installed, and enabled.

Instead you need to use db_query() function. (the comments are speaking of a way to include default data by prviding it to hook_schema() serialized, but i've found no documentation on this.)

However, would you be using (the development version of) Drupal 7, you want to look at the db_insert() function instead.

alexanderpas