1) モジュールを最初にインストールして有効にしたときに、新しいデータベース テーブルを作成するのに最適な場所はどこですか? 外部ソースからデータを取得する必要があり、ユーザーがカスタム モジュールをインストール/有効化するときに透過的に実行したいと考えています。
{mymodule}_schema() でスキーマを作成し、drupal_install_schema({tablename}); を実行します。hook_install で。次に、drupal_write_record を使用して hook_enable にテーブルを設定しようとしました。
テーブルが作成されたことを確認しました。hook_enable を実行してもエラーは発生しませんが、新しいテーブルにクエリを実行しても、行が返されません。空です。
私が試したコードのバリエーションの1つを次に示します。
/**
* 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 は次のとおりです。
function ncbi_subsites_install() {
drupal_install_schema('ncbi_subsites');
}
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 );
}
}
誰かが私に欠けているものを教えてもらえますか?