Doubt about attributes (getting the brand)
-
26-02-2021 - |
Question
I am a little confused about attributes whether they are simple eav attributes or not.
I dont fully understand when an attribute has option or option value. How can i check it?
So, for example in this case i am trying to obtain the brand name so i did this query:
SELECT main.sku, option_value_ez_catalog_product_brand.value AS ez_catalog_product_brand, code_status.value AS code_status
LEFT JOIN eav_attribute attribute_code_status ON attribute_code_status.attribute_code = 'status' and attribute_code_status.entity_type_id = 4
LEFT JOIN eav_attribute attribute_ez_catalog_product_brand ON attribute_ez_catalog_product_brand.attribute_code = 'ez_catalog_product_brand' and attribute_code_status.entity_type_id = 4
LEFT JOIN catalog_product_entity_int code_status ON main.entity_id = code_status.entity_id and code_status.attribute_id = attribute_code_status.attribute_id
LEFT JOIN catalog_product_entity_int ez_catalog_product_brand ON main.entity_id = ez_catalog_product_brand.entity_id and ez_catalog_product_brand.attribute_id = attribute_ez_catalog_product_brand.attribute_id
LEFT JOIN eav_attribute_option option_ez_catalog_product_brand on option_ez_catalog_product_brand.attribute_id = ez_catalog_product_brand.attribute_id and option_ez_catalog_product_brand.option_id = ez_catalog_product_brand.value
LEFT JOIN eav_attribute_option_value option_value_ez_catalog_product_brand on option_ez_catalog_product_brand.option_id = option_value_ez_catalog_product_brand.option_id
Is that okay? How can i know if an attribute is simple or not?
What about other attributes like : manufacturer, volume, description, prices and so on?
Could you please help me?
Solution
You can identify the type of an EAV attribute from its settings in the eav_attribute
table:
backend_type = int
means that the attribute is of type integer and the value is stored in the table [entity_type_code]_entity_int
, in case of a product attribute from
catalog_product_entity_int
. (For other backend types there are corresponding tables, backend_type = varchar
has catalog_product_entity_varchar
etc. backend_type = static
attributes are stored in the entity_table catalog_product_entity
- e.g. sku
).
If an integer attribute has frontend_type = select
that means that some kind of integer key is stored in [entity_type_code]_entity_int
and there is a related value to that key.
For this type of attributes there is usually a setting in the source_model
column. Attributes with option values usually have source_model = eav/entity_attribute_source_table
. Other types of backend models store the data in other tables - where and how exatly the data is stored can be seen in the corresponding Magento class (i.e. in case of eav/entity_attribute_source_table
Mage_Eav_Model_Entity_Attribute_Source_Table
).
Regarding your select:
To get the option values of an product attribute with options you will have to join 4 tables:
eav_attribute
,catalog_product_entity_int
,eav_attribute_option
andeav_attribute_option_value
and you will have to do that for every attribute. You may shortcut that by hardcoding the attribute_id
in your statement and skip the table eav_attribute
, but the other 3 tables need to be there.
Your statement is correct, you have just a little copy & paste error:
In the second join you use attribute_code_status.entity_type_id = 4
instead of attribute_ez_catalog_product_brand.entity_type_id = 4
.