0

I've used orConditionGroups with queries before. Specifically with entity queries. But now I have a custom sql table I created separate from drupal but still on the same database I have access to. I want to update a row if any of the fields are different but I'm having generating the or statement. Here was my attempt.

$connection = \Drupal::database();
$orConditionGroup = $connection->orConditionGroup()->condition('address', $fulladdress, '!=')->condition('full_name', $fullname, '!=')->condition('telephone', $workphone, '!=');
  $connection->update($table)->fields(['full_name' => $fullname, 'address' => $fulladdress, 'telephone' => $workphone, 'email' => $email])->condition('id', $id)->condition($orConditionGroup);

But I get the error

undefined method Drupal\Core\Database\Driver\mysql\Connection::condition()

Which makes sense since the connection class isn't a query object on its own, but not sure how else to tackle this. How would I create an orConditionGroup for just a plain direct sql query that isn't a drupal entity or anything like that?

1 Answer 1

2

I was able to get it to work by added the select method to the connection object like so:

$orConditionGroup = $connection->select($table)
                    ->orConditionGroup()
                    ->condition('address', $fulladdress, '!=')
                    ->condition('full_name', $fullname, '!=')
                    ->condition('telephone', $workphone, '!=');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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