Community

hook_views_data(): Can I create a view with the results of an INNER JOIN?

Hi

Reading Describing tables to Views, I want to understand of the utility of the join attribute.

When describing a view using hook_views_data(), can I define a view that is an INNER JOIN between two tables? If so, then how?

For example, let us say I want to define a view through hook_views_data() that is a join between two simple tables location and region.

TABLE location has a region_id value, and TABLE region has an associated region_name for every region_id.

What I want to know is if I can add the INNER JOIN relation to this view by showing region.region_name instead of location.region_id.

After I had defined a join relation within TABLE location: $data['location']['table']['join']['region'], I thought maybe is would see something dealing with this defined Relationship within /admin/build/views/edit/ for a view created with Location fields, but I did not.

hook_views_data() implementation of TABLE location. In this implementation, :

<?php
function location_views_data() {
 
$data['location']['table']['group'] = t('group_Locations');

 
$data['location']['table']['base'] =  array(
         
'field' => 'location_id',
         
'title' => t("Locations"),
         
'help'  => t("Locations that are associated with Projects."),
         
'weight' => -10,
        );
       
 
$data['location']['table']['join']['region'] = array(
     
'left_field' => 'region_id',
     
'field' => 'region_id',
    );
     
 
$data['location']['location_name'] = array(
       
'title' => t('Location Name'),
       
'help' => t('Location Name requires to be Unique.'),
       
'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['location']['region_id'] = array(
       
'title' => t('Region ID'),
       
'help' => t('Auto-Assigned ID from Region'),
       
'field' => array(
         
'handler' => 'views_handler_field_numeric',
         
'click sortable' => TRUE,
         ),
       
'filter' => array(
         
'handler' => 'views_handler_filter_numeric',
        ),
       
'sort' => array(
         
'handler' => 'views_handler_sort',
        ),
      );
    
  return
$data;
}
?>

This is the hook_views_data() implementation of TABLE region:

<?php
function region_views_data() {
 
$data['region']['table']['group'] = t('group_Regions');

 
$data['region']['table']['base'] =  array(
         
'field' => 'region_id',
         
'title' => t("Regions"),
         
'help'  => t("Regions that are associated with Locations."),
         
'weight' => -10,
        );
     
 
$data['region']['region_id'] = array(
       
'title' => t('Region ID'),
       
'help' => t('Auto-Assigned ID'),
       
'field' => array(
         
'handler' => 'views_handler_field_numeric',
         
'click sortable' => TRUE,
         ),
       
'filter' => array(
         
'handler' => 'views_handler_filter_numeric',
        ),
       
'sort' => array(
         
'handler' => 'views_handler_sort',
        ),
      );
     
 
$data['region']['region_name'] = array(
       
'title' => t('Region Name'),
       
'help' => t('Region Name requires to be Unique.'),
       
'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',
        ),
      );
    
  return
$data;
}
?>

Am I off course with this understanding with hook_views_data()?

Thanks

Jeff in Seattle