SELECT ea.attribute_code, ea.backend_type,
CASE ea.backend_type
WHEN 'varchar' THEN ce_varchar.value
WHEN 'int' THEN ce_int.value
WHEN 'decimal' THEN ce_decimal.value
WHEN 'datetime' THEN ce_datetime.value
WHEN 'text' THEN ce_text.value
ELSE 'Unsupported type'
END as value
FROM eav_attribute ea
LEFT JOIN catalog_product_entity_varchar ce_varchar ON ea.attribute_id = ce_varchar.attribute_id AND ce_varchar.entity_id = '4905'
LEFT JOIN catalog_product_entity_int ce_int ON ea.attribute_id = ce_int.attribute_id AND ce_int.entity_id = '4905'
LEFT JOIN catalog_product_entity_decimal ce_decimal ON ea.attribute_id = ce_decimal.attribute_id AND ce_decimal.entity_id = '4905'
LEFT JOIN catalog_product_entity_datetime ce_datetime ON ea.attribute_id = ce_datetime.attribute_id AND ce_datetime.entity_id = '4905'
LEFT JOIN catalog_product_entity_text ce_text ON ea.attribute_id = ce_text.attribute_id AND ce_text.entity_id = '4905'
WHERE ea.entity_type_id = '4';
La query che hai fornito interroga diverse tabelle per recuperare gli attributi e i loro valori per un dato prodotto in Magento. Ecco le tabelle coinvolte in questa query e una breve descrizione del loro scopo:
eav_attribute:
Questa tabella contiene informazioni sugli attributi, come l’ID dell’attributo (attribute_id), il codice dell’attributo (attribute_code), e il tipo di backend (backend_type) che indica il tipo di dato dell’attributo (varchar, int, decimal, datetime, text).
catalog_product_entity_varchar:
Questa tabella memorizza i valori degli attributi di tipo ‘varchar’ (stringhe di testo breve) per i prodotti. Viene associata alla tabella eav_attribute tramite attribute_id per ottenere i valori degli attributi di tipo varchar.
catalog_product_entity_int:
Questa tabella memorizza i valori degli attributi di tipo ‘int’ (numeri interi) per i prodotti. Come la tabella catalog_product_entity_varchar, viene associata alla tabella eav_attribute per ottenere i valori degli attributi di tipo intero.
catalog_product_entity_decimal:
Questa tabella è utilizzata per memorizzare i valori degli attributi di tipo ‘decimal’ (numeri decimali) per i prodotti. È associata a eav_attribute per ottenere i valori degli attributi di tipo decimale.
catalog_product_entity_datetime:
Questa tabella memorizza i valori degli attributi di tipo ‘datetime’ (data e ora) per i prodotti. Viene associata a eav_attribute per ottenere i valori degli attributi di tipo datetime.
catalog_product_entity_text:
Questa tabella è utilizzata per memorizzare i valori degli attributi di tipo ‘text’ (stringhe di testo lunghe) per i prodotti. Viene associata a eav_attribute per ottenere i valori degli attributi di tipo testo.
In questa query, la tabella eav_attribute viene utilizzata come tabella principale da cui vengono estratti i codici degli attributi e i tipi di backend. Le altre tabelle (catalog_product_entity_varchar, catalog_product_entity_int, catalog_product_entity_decimal, catalog_product_entity_datetime, catalog_product_entity_text) vengono poi associate a eav_attribute per recuperare i valori specifici degli attributi per un dato prodotto, identificato dall’entity_id (‘4905’ in questo caso) e filtrati per tipo di entità (entity_type_id = ‘4’, tipicamente per i prodotti).
catalog_product_entity
catalog_product_entity_varchar
entity_id ==> CHIAVE PRIMARIA
SELECT
entity.entity_id,
entity.sku,
varchar_table.attribute_id,
varchar_table.value AS attribute_value,
attribute.attribute_code AS attribute_name
FROM
catalog_product_entity AS entity
INNER JOIN
catalog_product_entity_varchar AS varchar_table ON entity.entity_id = varchar_table.entity_id
LEFT JOIN
eav_attribute AS attribute ON varchar_table.attribute_id = attribute.attribute_id
WHERE
entity.entity_id = 5305
tabella eav_attribute
Ci sono dentro tutte le label utili nella colonna attribute_code, fanno tutti riferimento al valore della colonna attribute_id
tabella catalog_product_entity_text
ulteriori informazioni prodotto: chiavi entity_id, attribute_id (recuperabile valore da eav_attribute)
tabella catalog_product_entity_varchar
tabella eav_attribute_option_value
copertina flessibile: option_id = 8
copertina rigida: option_id = 9
B/N: option_id = 4 ==> attribute_name = stampa
4 colori = option_id = 3
ebook = option_id = 10
lingua italiano = option_id = 7
lingua inglese = option_id = 6
SELECT
text_table.entity_id AS ID_PRODOTTO,
attribute.attribute_code,
text_table.value AS text_attribute_value
FROM
eav_attribute AS attribute
LEFT JOIN
catalog_product_entity_text AS text_table
ON attribute.attribute_id = text_table.attribute_id
WHERE
text_table.entity_id = 5305
La query estrae:
description
short_description
meta_keyword
indice
risorse
tagricerca (skip)
ebook_name
Prodotti Abilitati o disabilitati
SELECT
entity_id
FROM
catalog_product_entity
WHERE
entity_id IN (
SELECT entity_id
FROM catalog_product_entity_int
WHERE attribute_id = (
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'status'
)
AND value != 1 -- Valore che indica lo stato "Abilitato"
# AND value = 1 -- abilitato
)
SELECT
entity.entity_id AS ID_PRODOTTO,
entity.sku AS ISBN,
IFNULL(stock_table.value, 0) AS ABILITATO,
visibility_table.value AS VISIBILITA
FROM
catalog_product_entity AS entity
LEFT JOIN
catalog_product_entity_int AS stock_table
ON entity.entity_id = stock_table.entity_id
AND stock_table.attribute_id = (
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'status'
)
LEFT JOIN
catalog_product_entity_int AS visibility_table
ON entity.entity_id = visibility_table.entity_id
AND visibility_table.attribute_id = (
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'visibility'
)
WHERE
entity.entity_id IN (4693,1608,4694,1606,1610,4692)
VIBILITÀ VALORI:
1: non visibile
2: catalogo
3: ricerca
4: catalogo e ricerca
ABILITATO VALORI:
1: abilitato
2: disabilitato