【发布时间】:2011-12-25 02:28:50
【问题描述】:
我已经构建了一个在客户端(JavaScript 和 HTML)上运行的应用程序,它需要访问和更新服务器上的数据。它有一个由 5 个表组成的模式。我已经准确定义了它们在 JSON 中应该是什么样子。我希望这些可以作为从 Drupal 模块提供的 JSON 服务使用。我了解如何使用 drupal_json_output 来提供结果。我只是找不到一种简单的方法来确保为他们创建数据库表,然后从中添加和删除项目。我想保持 Drupal 与底层数据库的独立性。我不需要任何搜索功能、表单功能等。我只想使用 Drupal 的数据库抽象。
目前我在安装文件中尝试了以下内容:
/**
* Implements hook_schema
*/
function rcsarooms_schema(){
$schema = array();
$schema['rcsarooms'] = array(
'description' => 'Stores the structured information about the rooms and staircases.',
'fields' => array(
'ID' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'description' => 'Primary Key: used in the URLs to identify the entity.'
),'Name' => array(
'type' => 'varchar',
'length' => 200,
'not null' => TRUE,
'description' => 'The name used for links in the navigation menues.'
),'ParentID' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'description' => 'The ID of the parent element or "Root" if this is a root element'
),'Type' => array(
'type' => 'varchar',
'length' => 15,
'not null' => TRUE,
'description' => 'page, staircase, house, room or special'
),'BathroomSharing' => array(
'type' => 'int',
'description' => 'The number of people the bathroom is shared with (0 for unknown)'
),'RentBand' => array(
'type' => 'int',
'description' => 'The ID of the rent band the room is in.'
),'Floor' => array(
'type' => 'int',
'description' => 'The floor number (0 is courtyard level).'
)
),
'primary key' => array('ID')
);
return $schema;
}
我的模块文件中有以下内容:
/**
* Implements hook_menu
*/
function rcsarooms_menu(){
$items['rcsarooms'] = array(
'page callback' => 'rcsarooms_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
function rcsarooms_callback(){
drupal_json_output(db_query("SELECT * FROM {rcsarooms}"));
drupal_exit();
return;
}
当我尝试导航到 rcsarooms 时,会出现以下错误: PDOException:SQLSTATE [42S02]:未找到基表或视图:1146 表 'db.rcsarooms' 不存在:SELECT * FROM {rcsarooms}; rcsarooms_callback()中的数组( )
【问题讨论】:
-
您能否澄清一下,您本质上是在问如何使用 Drupal 数据库层连接到外部数据库吗?还是我错过了重点?
-
@Clive 没有抓住重点——我问的是如何从一个模块在 Drupal 的数据库中创建一个表。 IE。我已经从概念上设计了我想要的东西,一张房间表,每个房间都有一个 ID、名称、价格等。我想将该信息存储在 Drupal 的数据库中,然后使其可供客户端上运行的一些 JavaScript 代码访问。
-
啊,这就是我最初的想法,我已经准备好了答案:)
标签: php mysql drupal drupal-7 drupal-modules