I am trying to write Views integration code for my custom tables. The integration (generated with this module) works fine when I only want to list the content in one single table but I do not manage to make a join in the created code and than get this to work in Views. I have make a simple example module to test this and pasted this in the created code in tabeller.views.inc after the base section
'join' => array(
'table_last' => array(
'handler' => 'views_join',
'left_table' => 'table_first',
'left_field' => 'last_id',
'field' => 'last_id',
),
),
The tables shows up in Views but I do not get the relationships. Is it any wrong with my code or have I misunderstood the concept in Views?
It would be very helpful with a small join example in the documentation.
The hole file tabeller.views.inc:
$data['table_first'] = array(
'table' => array(
'base' => array(
'field' => 'id',
'title' => 'Table First',
'help' => 'List first names',
),
'join' => array(
'table_last' => array(
'handler' => 'views_join',
'left_table' => 'table_first',
'left_field' => 'last_id',
'field' => 'last_id',
),
),
'group' => 'Table First',
),
'id' => array(
'title' => 'Id',
'help' => 'Primary key',
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => FALSE,
),
),
'last_id' => array(
'title' => 'Last Id',
'help' => 'Links to last name',
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => FALSE,
),
),
'first' => array(
'title' => 'First',
'help' => 'First name',
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => FALSE,
),
),
);
$data['table_last'] = array(
'table' => array(
'base' => array(
'field' => 'last_id',
'title' => 'Table Last',
'help' => 'List names',
),
'group' => 'Table Last',
),
'last_id' => array(
'title' => 'Last Id',
'help' => 'Primary key',
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => FALSE,
),
),
'name' => array(
'title' => 'Name',
'help' => 'Last name',
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => FALSE,
),
),
);
return $data;
}
Comments
Comment #1
eelkeblokThanks for using the module, I hope it was helpful.
Your first stop for understanding the Views API is the information in Advanced Help. Having said that, joins in Views are a little tricky. I have been messing with them myself recently and lost far too much time with it. The code that comes with core Views for the standard Drupal tables can be very helpful, because it has a lot of comments. Studying the code of the files and files_usage tables was instrumental in my own understanding of how this stuff works.
One important part to keep in mind is that the way you are describing joins (with a joins clause in the table definition) is considered implicit. It means that whenever you create a View of table_last items, table_first fields will be added automatically. If you want to have relationships you need to add explicitly, you need to add a relationship clause, either to an existing field, or to a pseudo field that is only there to describe the relationship (you will need a 'real field' item to define the actual field needed for the relationship).
Either way, this is not really in scope for Schema Views module, which explicitly excludes the extra intelligence of relationships and the like. I'll move it to the Views project as a support request.
Good luck with getting your head around joins and relationships.
Comment #1.0
eelkeblokChanging issue text a little to take emphasis of how the basic integraiton code was generated, as it really isn't relevant to the issue.
Comment #2
mustanggb commented