1

Compare this code

    $productIdQuery = $this->productCollectionFactory->create()
        ->addAttributeToSelect('customer_group')
        ->addAttributeToFilter([
            ['attribute' => 'customer_group', 'finset' => (int)$customerGroupId], 
            ['attribute' => 'customer_group', 'eq' => ''],
            ['attribute' => 'customer_group', 'null' => true],
        ]);

to this code

    $productIdQuery = $this->productCollectionFactory->create()
        ->addAttributeToSelect('customer_group')
        ->addAttributeToFilter([
            ['attribute' => 'customer_group', 'null' => true],
            ['attribute' => 'customer_group', 'eq' => ''],
            ['attribute' => 'customer_group', 'finset' => (int)$customerGroupId], 
        ]);

It gives different results. In the old code, rows with missing attribute value entries in the data base are missing.

Is this intended?

1

The resulting queries differ:

SELECT `e`.*,
       IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value) AS `customer_group`
FROM `catalog_product_entity` AS `e`
         INNER JOIN `catalog_product_entity_varchar` AS `at_customer_group_default`
                    ON (`at_customer_group_default`.`entity_id` = `e`.`entity_id`) AND
                       (`at_customer_group_default`.`attribute_id` = '161') AND
                       `at_customer_group_default`.`store_id` = 0
         LEFT JOIN `catalog_product_entity_varchar` AS `at_customer_group`
                   ON (`at_customer_group`.`entity_id` = `e`.`entity_id`) AND
                      (`at_customer_group`.`attribute_id` = '161') AND (`at_customer_group`.`store_id` = 1)
WHERE ((FIND_IN_SET(0, IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value))) OR
       (IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value) = '') OR
       (IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value) IS NULL))

vs.

SELECT `e`.*,
       IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value) AS `customer_group`
FROM `catalog_product_entity` AS `e`
         LEFT JOIN `catalog_product_entity_varchar` AS `at_customer_group_default`
                   ON (`at_customer_group_default`.`entity_id` = `e`.`entity_id`) AND
                      (`at_customer_group_default`.`attribute_id` = '161') AND
                      `at_customer_group_default`.`store_id` = 0
         LEFT JOIN `catalog_product_entity_varchar` AS `at_customer_group`
                   ON (`at_customer_group`.`entity_id` = `e`.`entity_id`) AND
                      (`at_customer_group`.`attribute_id` = '161') AND (`at_customer_group`.`store_id` = 1)
WHERE ((IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value) IS NULL) OR
       (IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value) = '') OR
       (FIND_IN_SET(0, IF(at_customer_group.value_id > 0, at_customer_group.value, at_customer_group_default.value))))

The JOIN types differ.

This seems to happen here

vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection.php:1505

if (isset($condition['null'])) {
    $joinType = 'left';
}

So if the first condition is a "IS NULL" condition, Magento builds the join implicitly as left join.

A better solution might be to write

$productIdQuery = $this->productCollectionFactory->create()
    ->addAttributeToSelect('customer_group')
    ->addAttributeToFilter([
        ['attribute' => 'customer_group', 'finset' => (int)$customerGroupId], 
        ['attribute' => 'customer_group', 'eq' => ''],
        ['attribute' => 'customer_group', 'null' => true],
    ], null, 'left');

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.