我使用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;
}
我的映射有什么问题吗?当我创建一个View时,我正在尝试一个简单的Unformatted List并只显示带有标签的每个字段。显示数据的唯一字段是id
,我的表中的所有id
值都在那里。我尝试在视图中添加一个过滤器,以便课程部门!= 空白和课程名称!= 空白,这并没有消除任何结果(基于{{1显示的s)。这是View正在生成的查询:
id
当我在phpMyAdmin中运行此操作时,只需从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 '\\') ))
左右移除{
和}
,它就会返回结果,并且每列中都有数据。
编辑:或许可以说我为同一个数据库中的另一个表创建了另一个自定义View模块,它运行正常。我使用该模块作为此模块的基础,更改模块名称,功能前缀等。
答案 0 :(得分:2)
清除缓存,禁用模块'my_module',保存,然后启用它,保存
在我的开发中,我必须添加此钩子才能使其正常工作
function my_module_views_api() {
return array('api'=>2.0);
}