Skip to content

Filtering a collection on a custom attribute and a custom store_id generate wrong sql request #23720

@alexmtch

Description

@alexmtch

Preconditions (*)

  1. Magento CE 2.3.2
  2. Php 7.1
  3. developer (or production ?) mode

Steps to reproduce (*)

  1. Create a custom attribute with a store_id scope
  2. Create a product, attach it to a new store view (not the website or the store group) and set a value for the specific store to the custom attribute
  3. Create a product collection on a custom attribute following this code :
$products = $this->productFactory->create()
                ->getCollection()
                ->addStoreFilter(2)
                ->addAttributeToFilter('custom_attribute', '123');

Expected result (*)

  1. In my case i expected a result of a collection which contains 1 item

Actual result (*)

  1. I got an empty collection

After a workaround i retrieve the sql request

SELECT `e`.*, IF(at_custom_attribute.value_id > 0, at_custom_attribute.value, at_custom_attribute_default.value) AS `custom_attribute` 
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_website` AS `product_website` ON product_website.product_id = e.entity_id AND product_website.website_id = 2
INNER JOIN `catalog_product_entity_varchar` AS `at_custom_attribute_default` ON (`at_custom_attribute_default`.`entity_id` = `e`.`entity_id`) AND (`at_custom_attribute_default`.`attribute_id` = '163') AND `at_custom_attribute_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_varchar` AS `at_custom_attribute` ON (`at_custom_attribute`.`entity_id` = `e`.`entity_id`) AND (`at_custom_attribute`.`attribute_id` = '163') AND (`at_custom_attribute`.`store_id` = 2) WHERE (IF(at_custom_attribute.value_id > 0, at_custom_attribute.value, at_custom_attribute_default.value) = '123')

You see on the line 4 a join condition on the store id where the value is 0 while it should be 2. I fixed the sql request and run into mysql and i got the correct sql result.

After a workaround this is generated by the method _joinAttributeToSelect of this file vendor/magento/module-catalog/Model/ResourceModel/Collection/AbstractCollection.php

Update

I see in this method this comment

/**

  • Add joining default value for not default store
  • if value for store is null - we use default value
    */

I suppose by this comment that this declaration

$defCondition .= $connection->quoteInto(
                " AND " . $connection->quoteColumnAs("{$defAlias}.store_id", null) . " = ?",
                $this->getDefaultStoreId()
            );

should be

$defCondition .= $connection->quoteInto(
                " AND " . $connection->quoteColumnAs("{$defAlias}.store_id", null) . " = ?",
                $storeId ? $storeId : $this->getDefaultStoreId()
            );

What do you think about this ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: OrderComponent: BackendIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: ready for devReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releaseReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S2Major restrictions or short-term circumventions are required until a fix is available.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions