请解释 Drupal 模式和 drupal_write_record
1) 首次安装、启用模块时,填充新数据库表的最佳位置在哪里?我需要从外部源获取一些数据,并希望在用户安装/启用我的自定义模块时透明地执行此操作。
我在 {mymodule}_schema() 中创建模式,执行 drupal_install_schema({tablename});在钩子安装中。然后我尝试使用 drupal_write_record 填充 hook_enable 中的表。
我确认表已创建,当 hook_enable 执行时我没有收到任何错误,但是当我查询新表时,我没有返回任何行——它是空的。
这是我尝试过的代码的一种变体:
/**
* 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 );
}
}
有人能告诉我我错过了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 ncbi_subsites_get_subsites() 不在 .install 文件中,则需要将其包含在模块中的任何文件。否则,它不会返回任何内容,在这种情况下尝试转储 $subsites 并退出。
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.
我认为答案是 drupal_write_record 不适用于安装或启用挂钩。我认为在启用或安装时,你必须编写SQL。这是我从阅读一些提到该架构在这些挂钩中不可用的帖子中得到的印象。
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.
首先(假设是 Drupal 6),
drupal_write_record()
< /a> 无法从 hook_install() 调用,因为 Drupal 找不到从该模块定义的数据库模式,该模块仍将被安装和启用。相反,您需要使用
db_query()
函数。 (评论谈到了一种通过将默认数据提供给< code>hook_schema() 序列化,但我没有找到这方面的文档。)但是,您是否正在使用 Drupal 7(的开发版本),您想看看改为
db_insert()
函数。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 tohook_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.