Understanding Views Relationships

Source: http://drupal.org/node/1578586

Relationships are used to allow Views to bring in data that is associated with the data already available in the view. A comment view could, for example, use the relationship comment: content to tap into data about the node for each comment. A term view could use the relationship taxonomy: parent term to tap into data from the parent term of each listed term.

People comfortable with writing SQL queries will recognize the relationships as joins. You add, edit and delete relationships in the same way as filter, view fields and sort criteria by using the add button and its related menu.

Image removed.

To look at the actual SQL generated by a view follow the steps below

  1. Goto: admin/structure/views/settings
  2. Under 'Live preview settings'
  3. Make sure 'Show information and statistics about the view during live preview' option is checked
  4. Check the 'Show the SQL query' option
  5. Save

Relationship settings:

You add, edit and delete relationships in the same way as filter, view fields and sort criteria by using the add button and its related menu. These are the settings for relationships:

  • Identifier: This is the name that will be used for the relationship within the Views administration interface.
  • Require this relationship: Checking this option will make the view exclude items where these relationships cannot be fulfilled.
    For the relationship taxonomy: parent term, for example, it would mean that terms without parent terms would be excluded.

A view with relationships will, for each result in the view, not only have data for the base object of the view, but also for the objects described by the relationships. When editing the configuration of data fields there is (where applicable) an option relationships in the fieldset more. This setting can be used to select which object should be used for this data field. This means that in a comment view utilizing the comment: content relationship, you can choose to filter on node type. If you are including the relationship comment: user, you could sort the results by the name of the user writing the comment.

Data fields can be tied to the base object of the view, or to any of the objects provided by relationships. When creating a view, you select the base table for the view, comments, users, nodes, etc., to decide what will be the view's base objects. Some functionality in Views always acts on the base object, ignoring all relationships. An example of this is the distinct option, hidden under query settings in the advanced section.

If you use the Entity reference, References and Relation modules to connect entities on your site, these connections will show up as relationships in Views.

Share this post