I have two tables that I have specified as join for as follows in hook_views_data:

  $data['market_data_summary']['table'] = array(
    'group' => t('Market Data'),
    'join'  => array(
      'market_data' => array(
        'left_field' => 'mmid',
        'field'      => 'mmid',
      ),
    ),
  );

My problem is that these two tables actually have a multiple-column key. How can I specify more than one key association? Currently I am joining ON the mmid field in both tables -- but ideally I'd like to be able to join ON mmid AND pid AND cid (for example).

Comments

dawehner’s picture

See this comment

  $data['node']['table']['join'] = array(
    // this explains how the 'node' table (named in the line above)
    // links toward the node_revision table.
    'node_revision' => array(
      'handler' => 'views_join', // this is actually optional
      'left_table' => 'node_revision', // Because this is a direct link it could be left out.
      'left_field' => 'nid',
      'field' => 'nid',
      // also supported:
      // 'type' => 'INNER',
      // 'extra' => array(array('field' => 'fieldname', 'value' => 'value', 'operator' => '='))
      //   Unfortunately, you can't specify other tables here, but you can construct
      //   alternative joins in the handlers that can do that.
      // 'table' => 'the actual name of this table in the database',
     ),
  );

I hope it helps you.

ISPTraderChris’s picture

The following works in a very simple view -- this makes use of the 'extra' parameter.

  $data['market_data_sources']['table'] = array(
    'group' => t('Market Data'),
    'join'  => array(
      'market_data' => array(
        'left_field' => 'source_id',
        'field'      => 'source_id',
        'extra'      => array(
          array(
            'field'    => 'source_id',
            'value'    => 'market_data.source_id',
            'operator' => '=',
            'numeric'  => true,
          ),
        )
      ),
    ),
  );

My concern with this approach is that I have to manually alias one of the tables (the table with the 'value') -- and I have a feeling this will break down when the view gets more complex. Is this still the best approach? Is there some way to force views to provide the alias it is using?

merlinofchaos’s picture

You may have to write your own views_join handler for that.

iamjon’s picture

Status: Active » Postponed (maintainer needs more info)

ISPTraderChris,
Were you ever able to solve the problem? If so please share so others can benefit, if not please update so we can better try help you.

iamjon’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)
ufku’s picture

Version: 6.x-2.6 » 8.x-3.x-dev
Component: Miscellaneous » Code
Category: Support request » Feature request
Issue summary: View changes
Status: Closed (cannot reproduce) » Active
MustangGB’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.3.x-dev
Component: Code » views.module

No idea why this was re-opened, but 8.x issues are handled by core.

dawehner’s picture

Status: Active » Fixed

There is no reason to open this up again.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.