Magento2: Resource method getAttributeRawValue for category entity is fail?

Created on 9 Aug 2016  路  5Comments  路  Source: magento/magento2

Issue with \Magento\Catalog\Model\ResourceModel\Category::getAttributeRawValue()

Preconditions

  1. Magento 2.1 stable version.
  2. Check please with the version without sample data.
  3. You have a category with ID = 1000 (any value, for example).
  4. You do not have a product with ID = 1000 (value as category ID above).

    Steps to reproduce

  5. In any place you do call (pseudo code):

\Magento\Catalog\Model\ResourceModel\Category::getAttributeRawValue(1000, 'name', 0);

Expected result

  1. You get category (ID 1000) name for the default store.

    Actual result

  2. You get nothing.

\Magento\Catalog\Model\ResourceModel\Product
and
\Magento\Catalog\Model\ResourceModel\Category
extends
\Magento\Catalog\Model\ResourceModel\AbstractResource

\Magento\Catalog\Model\ResourceModel\Product
and
\Magento\Catalog\Model\ResourceModel\Category
do not have own getAttributeRawValue method and use parent method.

This is the fragment of getAttributeRawValue method:

$select = $connection->select()
                    ->from(['default_value' => $table], ['attribute_id'])
                    ->join(
                        ['e' => $this->getTable('catalog_product_entity')],
                        'e.' . $this->getLinkField() . ' = ' . 'default_value.' . $this->getLinkField(),
                        ''
                    )->where('default_value.attribute_id IN (?)', array_keys($_attributes))
                    ->where("e.entity_id = :entity_id")
                    ->where('default_value.store_id = ?', 0);

$this->getTable('catalog_product_entity') - hard code - for category fail:

SELECT `default_value`.`attribute_id`, IF(store_value.value IS NULL, default_value.value, store_value.value) AS `attr_value` FROM `catalog_category_entity_varchar` AS `default_value`
 INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = default_value.entity_id
 LEFT JOIN `catalog_category_entity_varchar` AS `store_value` ON store_value.attribute_id IN (42) AND store_value.entity_id = e.entity_id AND store_value.store_id = 1 WHERE (default_value.attribute_id IN (42)) AND (e.entity_id = 1875) AND (default_value.store_id = 0) 

Deep:

FROM `catalog_CATEGORY_entity_varchar` AS `default_value`
 INNER JOIN `catalog_PRODUCT_entity` AS `e` ON e.entity_id = default_value.entity_id

In \Magento\Catalog\Model\ResourceModel\AbstractResource I see it twice:
line 505, 526

Fixed in 2.2.x Fixed in 2.3.x Clear Description Confirmed Format is valid Reproduced on 2.1.x bug report

Most helpful comment

@okorshenko I've seen that this bug was fixed on 13/06/2016, why is not merged yet?
here the commit: https://github.com/magento/magento2/commit/c5a09d4c7056176a282ced37c3a186a284ae2de0

All 5 comments

Shouldn't be there $this->getEntityTable() instead of $this->getTable('catalog_product_entity') ?

@okorshenko I've seen that this bug was fixed on 13/06/2016, why is not merged yet?
here the commit: https://github.com/magento/magento2/commit/c5a09d4c7056176a282ced37c3a186a284ae2de0

@sergei-sss, thank you for your report.
The issue is already fixed in develop branch, 2.2.0

@magento-engcom-team Given that this issue was reported against 2.1 branch, is there a plan to backport this to 2.1?

Hi, in my case I was querying a category attribute and was returned null even after this fix,

from the select used in \Magento\Catalog\Model\ResourceModel\AbstractResource:558 :

SELECT `default_value`.`attribute_id`, IF(store_value.value IS NULL, default_value.value, store_value.value) AS `attr_value` FROM `catalog_category_entity_varchar` AS `default_value`
 INNER JOIN `catalog_category_entity` AS `e` ON e.entity_id = default_value.entity_id
 LEFT JOIN `catalog_category_entity_varchar` AS `store_value` ON store_value.attribute_id IN (194) AND store_value.entity_id = e.entity_id AND store_value.store_id = :store_id WHERE (default_value.attribute_id IN (194)) AND (e.entity_id = :entity_id) AND (default_value.store_id = 0)

because of the clause AND (default_value.store_id = 0) and since default_value is the primary source table I get that if the a value exists only in store id != 0 then it's not gonna be retrieved

so is there an assumption that a default value should absolutely exist ? if yes, then maybe it should be inserted from the start in both stores. (Or should this be its separate issue ?)

Thank you !

Was this page helpful?
0 / 5 - 0 ratings