-1

I was following this tutorial on Drupal module development. It talks about database query joins in module development. The tutorial is made for Drupal 8 and I'm using Drupal 9.

Then I made this (look at the join part):

  protected function load () {
    $select = Database::getConnection()->select('rsvplist', 'r');
    $select.join('users_field_data', 'u', 'r.uid = u.uid');
    $select->join('node_field_data', 'n', 'r.nid = n.nid');
    $select->addField('u', 'name');
    $select->addField('n', 'title');
    $select->addField('r', 'mail');
    $entries = $select->execute()->fetchAll(\PDO::FETCH_ASSOC);
    return $entries;
  }

But get this error:

warning: join() expects at most 2 parameters, 3 given in Drupal\rsvplist\Controller\ReportController->load() (line 24 of /var/www/htdocs/drupal-modules/modules/custom/rsvplist/src/Controller/ReportController.php)
#0 /var/www/htdocs/drupal-modules/core/includes/bootstrap.inc(305): _drupal_error_handler_real(2, 'join() expects ...', '/var/www/htdocs...', 24)
#1 [internal function]: _drupal_error_handler(2, 'join() expects ...', '/var/www/htdocs...', 24, Array)
#2 /var/www/htdocs/drupal-modules/modules/custom/rsvplist/src/Controller/ReportController.php(24): join('users_field_dat...', 'u', 'r.uid = u.uid')
#3 /var/www/htdocs/drupal-modules/modules/custom/rsvplist/src/Controller/ReportController.php(52): Drupal\rsvplist\Controller\ReportController->load()
#4 [internal function]: Drupal\rsvplist\Controller\ReportController->report()
#5 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#6 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/Render/Renderer.php(573): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#7 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#8 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#9 /var/www/htdocs/drupal-modules/vendor/symfony/http-kernel/HttpKernel.php(158): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#10 /var/www/htdocs/drupal-modules/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#11 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /var/www/htdocs/drupal-modules/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /var/www/htdocs/drupal-modules/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/www/htdocs/drupal-modules/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /var/www/htdocs/drupal-modules/core/lib/Drupal/Core/DrupalKernel.php(706): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/www/htdocs/drupal-modules/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#20 {main}

Drupal 9's join don't accepts the third argument:

'r.uid = u.uid'

A made some research but I was not able to find an equivalent example about how to use join this way in Drupap 9 documentation and other sources.

How can I add joins to a query in Drupal 9?

2

In PHP:

  • . is the string concatenation operator, not the one to call an object method
  • join() is an alias for the implode() function, which effectively requires 2 arguments, not 3

This means that $select.join('users_field_data', 'u', 'r.uid = u.uid') (which PHP reads as $select . join('users_field_data', 'u', 'r.uid = u.uid')) is:

  • Converting the value contained in $select to a string
  • Concatenating that string with the string returned by implode()

The arguments accepted by implode() has been changed in the latest PHP versions, but the function has never accepted three arguments, and the first two arguments has never been two strings.

This explains the error you are getting.

In Drupal 9, if $select is an object implementing SelectInterface, $select->join() is an available method.

public function join($table, $alias = NULL, $condition = NULL, $arguments = []) {
  return $this->addJoin('INNER', $table, $alias, $condition, $arguments);
}

Instead of $select->join(), you could call $select->addJoin().
The first method doesn't require to specify the first or the last argument of $select->addJoin(), and it's the normally called method.

As side note, instead of using $select on database tables used for entity data, it's generally preferable to use the class returned from Drupal::entityQuery().

0

Solution:

In Drupal 9, I needed to replace joins so it works:

//$select.join('users_field_data', 'u', 'r.uid = u.uid');
$select->addJoin('inner','users_field_data','u','r.uid = u.uid');
//$select->join('node_field_data', 'n', 'r.nid = n.nid');
$select->addJoin('inner','node_field_data','n','r.nid = n.nid');

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.