2

The Views API in advanced help says "For Views to use your table, it has to either be a base table, or know how to link to an existing base table. Or sometimes both. "

I'm trying to add an "address" table to joins to the "person" table. The "person" table is a base table, "address" is not.

I can do this:

  $data['address'] = array(
    'table' => array(
      'group' => t('Address'),
      'join' => array(
        'person' => array(
          'left_field' => 'id',
          'field' => 'personID',
        ),
      ),
    ),
  );

However, this creates an implicit join relationship in views, meaning it will automatically make all fields in the "address" table available to the view and join the data with LEFT JOIN.

What I want to do is instead just add a relationship handler to the "personID" field so that the relationship is explicit, meaning a user must manually add the relationship.

I have successfully added this relationship handler to the "personID" field, and I can add the relationship. When adding the fields from this table, I can chose to use the relationship to add the field instead of the implicit relationship.

I want to completely remove the implicit relationship, and only allow users to add the data from the relationship handler. When I remove the 'join' data above, views won't allow me to add the relationship manually.

Is this possible? Or does that require that I define my joined table as a base table as well?

0

You should be able to add a relationship by using hook_views_data. You should however not use the join, but the relationship (right below the huge php comment on the linked page):

// Node ID table field.
$data['example_table']['nid'] = array(
  'title' => t('Example content'),
  'help' => t('Some example content that references a node.'),

  // Define a relationship to the {node} table, so example_table views can
  // add a relationship to nodes. If you want to define a relationship the
  // other direction, use hook_views_data_alter(), or use the 'implicit' join
  // method described above.
  'relationship' => array(
    'base' => 'node', // The name of the table to join with.
    'base field' => 'nid', // The name of the field on the joined table.
    // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
    'handler' => 'views_handler_relationship',
    'label' => t('Default label for the relationship'),
    'title' => t('Title shown when adding the relationship'),
    'help' => t('More information on this relationship'),
  ),
);
  • Yes, but you cannot describe a table to views without specifying it as an implicit join table or as a base table. I'm trying to avoid both. – Brian Mar 21 '14 at 19:36
  • I was assuming that having one base tabel, the persons table, one could relatie to any orher table. After reading your question I thought you only wanted to avoid setting the address table as base table. – Neograph734 Mar 21 '14 at 20:12
0

So, as it turns out, I do NOT need to describe the table as a base or join table (yay!). I just need to describe the fields. In my case, I needed to add a relationship handler to the person table like this:

$data['person']['address'] = array(
  'title' => t('Address'),
  'help' => t("Load person's address information from the address table."),
  'relationship' => array(
    'title' => t("Person's Address"),
    'label' => t("Address"),
    'base' => 'address',
    'base field' => 'personID',
    'relationship field' => 'id',
  ),
);

There are a couple of things at play here:

  1. Normally when adding fields to a table definition, the field has to exist on the database. As it turns out, you can describe "fake" fields as I've done above.
  2. This fake field adds a relationship to the table address joined by the local relationship field id.

With this in place, when person is the base table, I can add a relationship to address. See How do I define this simple views relationship? for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.