如何在 drupal 中对自定义实体定义和执行 CRUD

发布于 2024-12-22 22:24:41 字数 2665 浏览 3 评论 0原文

我构建了一个在客户端运行的应用程序(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() 中的数组 ( )

I've built an application to run on the client side (JavaScript & HTML) which needs to access and updated data on a server. It has a schema which consists of 5 tables. I've defined exactly what they should look like in JSON. I want these to be available as a JSON service served from a Drupal module. I understand how to use drupal_json_output to provide the results. I just can't find a simple way to ensure the database table is created for them and then to add and remove items from it. I'd like to maintain Drupal's independence from the underlying database. I don't need any search functionality, forms functionality etc. I just want to use Drupal's Database Abstraction.

At the moment I've tried the following in my install file:

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

And the following in my module file:

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

This gives the following error when I attempt to navigate to rcsarooms:
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.rcsarooms' doesn't exist: SELECT * FROM {rcsarooms}; Array ( ) in rcsarooms_callback()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-12-29 22:24:41

您可能正在寻找 <当您安装模块时,Drupal 将使用 code>hook_schema() 创建自定义表。它位于 mymodule.install 文件中。

Schema API 会告诉你您需要了解有关数据类型等的所有信息。

要从数据库中添加/删除项目,请使用 db_insert()< /a>, db_update()db_merge() 函数

You're probably looking for hook_schema() which Drupal will use to create your custom tables when you install your module. It goes in the mymodule.install file.

The Schema API will tell you everything you need to know about data types etc.

For adding/removing items from the database use the db_insert(), db_update() and db_merge() functions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文