Can anybody help us with understanding how to construct a join on 3 tables in Views 2 (Drupal 6)?
One thing we'd appreciate is links to any good documentation that covers 3 table joins. Most of what we've found ends at two tables.
The other thing we'd appreciate is your time in looking through our code (below) and identifying where we are going wrong.
We found the taxonomy module is recommended as having well-crafted views code so we looked at the taxonomy.views.inc file and modelled our code on that. However that code works and ours does not. :(
Here's the details:
We programmatically built a content type “consignment” and another content type “supplier”. We can display a list of consignments (with fields from both node and consignment), using Views. However, we are unable to include another content type / table (“supplier”) into the join.
Here is the SQL statement that we would write if we were writing this application at a lower level. This is the join we like to reproduce with Views:
SELECT node.nid AS nid,
c.cons_id,
c.cons_received,
supplier.name
FROM node n
LEFT JOIN consignment c ON n.vid = c.vid
LEFT JOIN supplier s ON s.vid = c.supplier_id
WHERE (node.status <> 0) AND (node.type in (consignment)) So far, no Supplier fields are listed in the views UI when trying to add fields. (With other versions of our code we can get the fields listed, but always the resulting SQL gives errors &/or does not include the second join).
Here's the code in our consignment.views.inc:
function consignment_views_data() {
$data['consignment']['table']['group'] = t('Consignment');
$data['consignment']['table']['join'] = array(
'node' => array(
'left_field' => 'vid',
'field' => 'vid'),
'supplier' => array(
'left_field' => 'supplier_id',
'field' => 'vid'),
);
$data['consignment']['cons_id'] = array(
'title' => t('Consignment ID'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE),
);
$data['consignment']['cons_received'] = array(
'title' => t('Consignment Received Date'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE),
);
$data['supplier']['table']['group'] = t('Consignment');
$data['supplier']['table']['base'] = array(
'field' => 'vid',
'title' => t('Supplier'),
);
$data['supplier']['name'] = array(
'title' => t('Consignment Supplier'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE),
);
return $data;
}
Any help would be greatly appreciated! What can we alter in the code to make it work?
Comments
I think you need to split
I think you need to split your join up...
See http://views-help.doc.logrus.com/help/views/api-tables
Worth a try.
pingers is essentially right,
I had a lot of trouble with it when views 2 was still in beta, but hopefully you should be fine if you follow that information now.