From 6eb7561bb15dbed531d62b227f590a15a8b73403 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 11 Jun 2024 17:13:42 +0100 Subject: [PATCH] Fix PGSQL selects --- src/Plugin.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 6ef81c4b9b..3031288ee7 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -771,11 +771,20 @@ function(DefineBehaviorsEvent $event) { Event::on(AddressQuery::class, AddressQuery::EVENT_AFTER_PREPARE, function(CancelableEvent $event) { /** @var UserQuery $sender */ $sender = $event->sender; - $sender->query->addSelect([ - 'isPrimaryBilling' => new Expression('IF([[commerce_customers.primaryBillingAddressId]] = [[addresses.id]], 1, 0)'), - 'isPrimaryShipping' => new Expression('IF([[commerce_customers.primaryShippingAddressId]] = [[addresses.id]], 1, 0)'), + if (Craft::$app->getDb()->getIsPgsql()) { + $sender->query->addSelect([ + 'isPrimaryBilling' => new Expression('CASE WHEN [[commerce_customers.primaryBillingAddressId]] = [[addresses.id]] THEN true ELSE false'), + 'isPrimaryShipping' => new Expression('CASE WHEN [[commerce_customers.primaryShippingAddressId]] = [[addresses.id]] THEN true ELSE false'), + ]); + } else { + $sender->query->addSelect([ + 'isPrimaryBilling' => new Expression('IF([[commerce_customers.primaryBillingAddressId]] = [[addresses.id]], 1, 0)'), + 'isPrimaryShipping' => new Expression('IF([[commerce_customers.primaryShippingAddressId]] = [[addresses.id]], 1, 0)'), + ]); + } - // Use this information to determine if the address is owned by a user + // Use this information to determine if the address is owned by a user + $sender->query->addSelect([ 'commerceCustomerId' => '[[commerce_customers.customerId]]', ]); $sender->query->leftJoin(Table::CUSTOMERS . ' commerce_customers', '[[commerce_customers.customerId]] = [[addresses.primaryOwnerId]]');