Problem/Motivation

Site was upgraded to Drupal 9 and most Drupal and CiviCRM functions seem to be working. I can manually create Drupal accounts that are matched to new contacts in CiviCRM, but the module doesn't work.

Steps to reproduce

Run a Manual Synchronization :

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /en/batch?id=17425&op=do_nojs&op=do
StatusText: 500 Service unavailable (with message)
ResponseText: The website encountered an unexpected error. Please try again later.Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'xxx.civicrm_uf_match' doesn't exist: SELECT "uf"."contact_id" AS "contact_id"
FROM
"civicrm_uf_match" "uf"
LEFT OUTER JOIN "civicrm_membership" "m" ON uf.contact_id = m.contact_id
WHERE ("m"."id" IS NOT NULL) AND ("m"."membership_type_id" IN (:db_condition_placeholder_0)); Array
(
[:db_condition_placeholder_0] => 2
)
in Drupal\civicrm_member_roles\CivicrmMemberRoles->getSyncContactIds() (line 184 of modules/contrib/civicrm_member_roles/src/CivicrmMemberRoles.php).

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kathc created an issue. See original summary.

edsel’s picture

I encountered the same issue. To fix, I navigated to http://sitename.com/civicrm/admin/setting/uf?reset=1 and copied the entire array for the $databases and pasted it into my settings.php. According to the docs, Drupal 9 should recognize the CiviCRM database automatically as long as you have the correct prefixes, but it seems this module doesn't support that yet and needs this deprecated method.

nickdjm’s picture

The original site referred to is using a separate DB for CiviCRM information. I believe this is the root of the issue.

When we look at the logs (including the error message posted) it is always using the Drupal database "name_of_drupal_db.civicrm_uf_match" instead of "name_of_civicrm_db.civicrm_uf_match".

jonhalle’s picture

This is because the code uses a database call instead of API. It can be fixed by changing the function getSyncContactIds in src/CivicrmMemberRoles.php. Sorry I don't know how to do a proper pull request:


// Find contacts with applicable types.
/*    $select = $this->database->select('civicrm_uf_match', 'uf')
      ->fields('uf', ['contact_id']);
    $select->leftJoin('civicrm_membership', 'm', 'uf.contact_id = m.contact_id');
    $select->isNotNull('m.id')->condition('m.membership_type_id', $types, 'IN');

    if ($limit) {
      $select->range(0, $limit)->orderRandom();
    }

    return $select->execute()->fetchCol();
 */

$this->civicrm->initialize();
$uFMatches = \Civi\Api4\UFMatch::get()
  ->addSelect('contact_id')
  ->addJoin('Membership AS membership', 'LEFT', ['membership.contact_id', '=', 'contact_id'])
  ->addWhere('membership.membership_type_id', 'IN', $types)
  ->setLimit($limit)
  ->execute()
  ->column('contact_id');

  return $uFMatches;
 }

With this fix there is no need to maintain the database array in settings.php (which used to be required for Views etc but is no longer needed in D9)

seamus_lee’s picture

I have attached a patch that I worked on for a client site that had this same problem, It is slightly different in that I stuck with using the database but I instantised 2 database arrays as per civicrm entity which provides d9 views integration and modified the database query appropriately. That being said I think with the API or database way it is a bit of 6 of 1 half a dozen of the other, I'll let the maintainers decide which way they want to go.

olivierh65 made their first commit to this issue’s fork.

olivierh65’s picture

In created a PR to implement corrections proposed by jonhalle.
I've not tested it on 2 databases (one for drupal, an another for civicrm).

nubeli’s picture

Title: Synchronization error » Manual synchronization error
Issue summary: View changes
nubeli’s picture

@seamus_lee your patch gives this error: "Class "Drupal\civicrm_member_roles\Database" not found". Need to add use Drupal\Core\Database\Database; I think.

nubeli’s picture

Status: Active » Reviewed & tested by the community

sadashiv’s picture

Released in 8.x-1.0-beta3

sadashiv’s picture

Status: Reviewed & tested by the community » Fixed

Marking this as fixed

Status: Fixed » Closed (fixed)

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