Drupal 7 - 带有自定义表的自定义视图,没有显示数据

发布于 2024-12-11 13:41:25 字数 4402 浏览 0 评论 0原文

我使用 Drupal 7 编写了一个与 Views 3 交互的模块,但是当我使用自定义表作为数据源创建视图时,没有数据显示。这是我的 MySQL 模式:

+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | int(11)    | NO   | PRI | NULL    | auto_increment |
| title       | mediumtext | NO   |     | NULL    |                |
| Department  | text       | NO   |     | NULL    |                |
| credits     | int(10)    | NO   |     | NULL    |                |
| description | longtext   | NO   |     | NULL    |                |
+-------------+------------+------+-----+---------+----------------+

这是 my_module.views.inc 中的钩子覆盖:

function my_module_views_data() {
  $tableName = 'My_Awesome_Table';
  $data = array();
  $data[$tableName]['table']['group'] = t('Courses');

  $data[$tableName]['table']['base'] = array(
    'field' => 'id', 
    'title' => t('Courses'), 
    'help' => t("Contains courses, departments, and descriptions.")
  );

  $data[$tableName]['title'] = array(
    'title' => t('Course name'), 
    'help' => t('Course name'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data[$tableName]['Department'] = array(
    'title' => t('Course department'), 
    'help' => t('Course department'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data[$tableName]['credits'] = array(
    'title' => t('Credits'), 
    'help' => t('Number of credit hours'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ),
    'argument' => array('handler' => 'views_handler_argument_numeric'),
    'filter' => array('handler' => 'views_handler_filter_numeric'),
    'sort' => array('handler' => 'views_handler_sort_numeric')
  );

  $data[$tableName]['description'] = array(
    'title' => t('Course description'), 
    'help' => t('Course description'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data[$tableName]['id'] = array(
    'title' => t('Unique identifier'),
    'help' => t('Primary key for table'),
    'field' => array('handler' => 'views_handler_field'),
    'argument' => array('handler' => 'views_handler_argument_numeric'),
    'filter' => array('handler' => 'views_handler_filter_numeric'),
    'sort' => array('handler' => 'views_handler_sort_numeric'));

  return $data;
}

我的映射看起来有问题吗?当我创建视图时,我正在尝试一个简单的无格式列表,并仅使用标签显示每个字段。唯一显示数据的字段是 id,我的表中的所有 id 值都在那里。我尝试向视图添加过滤器,以便课程部门 != blank 和课程名称 != blank,这并没有消除任何结果(基于 显示的 id)。以下是 Views 生成的查询:

SELECT My_Awesome_Table.title AS My_Awesome_Table_title,
  My_Awesome_Table.Department AS My_Awesome_Table_Department,
  My_Awesome_Table.description AS My_Awesome_Table_description,
  My_Awesome_Table.credits AS My_Awesome_Table_credits,
  My_Awesome_Table.id AS id
FROM {My_Awesome_Table} My_Awesome_Table
WHERE (( (My_Awesome_Table.Department NOT LIKE '' ESCAPE '\\') AND
  (My_Awesome_Table.title NOT LIKE '' ESCAPE '\\') ))

当我在 phpMyAdmin 中运行此查询时,只需从 My_Awesome_Table 周围删除 {} ,它就会返回结果,并在那里是每列中的数据。

编辑:也许可以说,我为同一数据库中的不同表制作了另一个自定义视图模块,并且它工作得很好。我使用该模块作为该模块的基础,更改了模块名称、函数前缀等。

I wrote a module to interface with Views 3 using Drupal 7, but when I create a view using my custom table as a data source, no data shows up. Here's my schema from MySQL:

+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | int(11)    | NO   | PRI | NULL    | auto_increment |
| title       | mediumtext | NO   |     | NULL    |                |
| Department  | text       | NO   |     | NULL    |                |
| credits     | int(10)    | NO   |     | NULL    |                |
| description | longtext   | NO   |     | NULL    |                |
+-------------+------------+------+-----+---------+----------------+

And here's the hook override in my_module.views.inc:

function my_module_views_data() {
  $tableName = 'My_Awesome_Table';
  $data = array();
  $data[$tableName]['table']['group'] = t('Courses');

  $data[$tableName]['table']['base'] = array(
    'field' => 'id', 
    'title' => t('Courses'), 
    'help' => t("Contains courses, departments, and descriptions.")
  );

  $data[$tableName]['title'] = array(
    'title' => t('Course name'), 
    'help' => t('Course name'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data[$tableName]['Department'] = array(
    'title' => t('Course department'), 
    'help' => t('Course department'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data[$tableName]['credits'] = array(
    'title' => t('Credits'), 
    'help' => t('Number of credit hours'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ),
    'argument' => array('handler' => 'views_handler_argument_numeric'),
    'filter' => array('handler' => 'views_handler_filter_numeric'),
    'sort' => array('handler' => 'views_handler_sort_numeric')
  );

  $data[$tableName]['description'] = array(
    'title' => t('Course description'), 
    'help' => t('Course description'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data[$tableName]['id'] = array(
    'title' => t('Unique identifier'),
    'help' => t('Primary key for table'),
    'field' => array('handler' => 'views_handler_field'),
    'argument' => array('handler' => 'views_handler_argument_numeric'),
    'filter' => array('handler' => 'views_handler_filter_numeric'),
    'sort' => array('handler' => 'views_handler_sort_numeric'));

  return $data;
}

Does something look wrong with my mapping? When I create a View, I'm trying a simple Unformatted List and just displaying each field with a label. The only field that shows up with data is id, and all of the id values from my table are there. I tried adding a Filter to the View so that Course Department != blank and Course Name != blank, which didn't eliminate any results (based on the ids that are shown). Here's the query Views is generating:

SELECT My_Awesome_Table.title AS My_Awesome_Table_title,
  My_Awesome_Table.Department AS My_Awesome_Table_Department,
  My_Awesome_Table.description AS My_Awesome_Table_description,
  My_Awesome_Table.credits AS My_Awesome_Table_credits,
  My_Awesome_Table.id AS id
FROM {My_Awesome_Table} My_Awesome_Table
WHERE (( (My_Awesome_Table.Department NOT LIKE '' ESCAPE '\\') AND
  (My_Awesome_Table.title NOT LIKE '' ESCAPE '\\') ))

When I run this in phpMyAdmin, just removing the { and } from around My_Awesome_Table, it returns results and there is data in each column.

Edit: it's perhaps relevant to say that I made another custom View module for a different table in the same database, and it works fine. I used that module as a base for this one, changing the module name, function prefixes, etc.

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

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

发布评论

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

评论(1

2024-12-18 13:41:25

清除缓存,禁用模块“my_module”,保存,然后重新启用它,保存

在我的开发中,我必须添加这个钩子才能使其工作

function my_module_views_api() {
    return array('api'=>2.0);
}

Clear cache, disable module 'my_module', save, then enable it back, save

In my development, I have to add this hook to make it works

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