Skip to content

Commit

Permalink
Fix PGSQL selects
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Jun 11, 2024
1 parent 2f50c9c commit 6eb7561
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]');
Expand Down

0 comments on commit 6eb7561

Please sign in to comment.