API page: https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%...

The example on Views join handler plugins in the documentation and as found in JoinPluginBase.php is incorrect.

The presented example code makes it appear as if INNER joins are the default:

INNER JOIN {two} ON one.field_a = two.field_b
The required php code for this kind of functionality is the following:

$configuration = array(
  'table' => 'two',
  'field' => 'field_b',
  'left_table' => 'one',
  'left_field' => 'field_a',
  'operator' => '=',
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);

This is wrong and can lead to nasty side effects.
The default is LEFT JOIN. If you want an INNER JOIN, you have to add the type of the join. Like this:

$configuration = array(
  'type' => 'INNER',
  'table' => 'two',
  'field' => 'field_b',
  'left_table' => 'one',
  'left_field' => 'field_a',
  'operator' => '=',
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);

Comments

jummonk created an issue. See original summary.

jummonk’s picture

Issue summary: View changes
dawehner’s picture

Good catch. Feel free to improve it!

jhodgdon’s picture

Issue tags: +Novice

Should be a good novice issue to fix this documentation.

leolandotan’s picture

Assigned: Unassigned » leolandotan

I'll work on this.

leolandotan’s picture

Assigned: leolandotan » Unassigned
Status: Active » Needs review
StatusFileSize
new1.22 KB

Here is a simple patch I provided based on the requirements specified to explicitly specify the inner join type.

Hope everything is in order.

Thanks!

jhodgdon’s picture

Title: Incorrect documentation on Views join handler plugins (default is LEFT JOIN) » Incorrect and unclear documentation on Views join handler plugins (default is LEFT JOIN)
Status: Needs review » Needs work

Thanks! This looks fine... Although maybe we could change one of the examples so that the example SQL uses LEFT JOIN and then the example PHP code leaves out the join type? That might make it clearer yet that the default is LEFT.

Also maybe we could just add a note paragraph after the one that explains the base class and annotation class, saying something like:

Note that the default join type is a LEFT join.

Which would make it even clearer.

And... since we're fixing up this documentation block anyway, there are a couple of things we could do to make this whole page a bit better formatted and clearer... The wording at the top of the examples is misleading because it implies that all of the examples are for the same SQL. Also, there is not any vertical space between the end of the code for one example and the beginning of the SQL for the next example, and the wording is very awkward...

So to fix these problems, I suggest that we have the introductory line before the examples section say something like:

Here are some examples of configuration for join plugins.

And then have each example follow a template like this:

For this SQL:
@code
...
@endcode
Use this configuration:
@code
...
@endcode

And follow the @endcode with a blank line before the next example, to introduce some vertical space.

Sound like a good idea?

leolandotan’s picture

Assigned: Unassigned » leolandotan

Thank you for the review and feedback @jhodgdon! Yes, these sounds like a great idea.

Here is the summary of the items to be worked on from @jhodgdon's recommendations:

  1. Change one of the examples so that the example SQL uses LEFT JOIN and then the example PHP code leaves out the join type
  2. Add a note paragraph after the one that explains the base class and annotation class, saying something like: "Note that the default join type is a LEFT join"
  3. The wording at the top of the examples is misleading because it implies that all of the examples are for the same SQL.
  4. There is not any vertical space between the end of the code for one example and the beginning of the SQL for the next example, and the wording is very
  5. I suggest that we have the introductory line before the examples section
  6. Follow the @endcode with a blank line before the next example, to introduce some vertical space.

For item #1 and #2, I'll try to apply them both.

Thanks!

leolandotan’s picture

Assigned: leolandotan » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.5 KB
new2.34 KB

Here I have made the changes as recommended by @jhodgdon.

In relation to item #1 and #2 in my outline, instead of adding the note in the class part I added it after the first code example showing the default(LEFT) join configuration.

Thanks!

jhodgdon’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs review » Reviewed & tested by the community

Wonderful, thanks! Also moving this to 8.1 because I believe the 8.0 branch is now closed.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 3c5be66 and pushed to 8.1.x and 8.2.x. Thanks!

This is rc eligible since it is a docs only change.

  • alexpott committed dd8056f on 8.2.x
    Issue #2699637 by leolando.tan, jhodgdon, jummonk: Incorrect and unclear...

  • alexpott committed 3c5be66 on 8.1.x
    Issue #2699637 by leolando.tan, jhodgdon, jummonk: Incorrect and unclear...

Status: Fixed » Closed (fixed)

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