Skip to content

Instantly share code, notes, and snippets.

@lnoering
Last active July 6, 2022 14:01
Show Gist options
  • Save lnoering/a5a062e39ee04a25e3fc90d9e92fe6ed to your computer and use it in GitHub Desktop.
Save lnoering/a5a062e39ee04a25e3fc90d9e92fe6ed to your computer and use it in GitHub Desktop.
Magento 2 - Get All Attribute values from entity by id

Magento 2

Get all eav data from specific product

use magento;

SET @ENTITY_TABLE = 'catalog_product_entity';
SET @COLUMNID = 'sku';
SET @VALUEID = 'INSIRA SKU AQUI';

-- Change by magento version
-- SET @EQUAL = 'entity_id';
SET @EQUAL = 'row_id';


SET @entityQuery = concat("SELECT * FROM (
    SELECT 
        ce.sku,
        ea.attribute_id,
        ea.attribute_code,
        CASE ea.backend_type
           WHEN 'varchar' THEN ce_varchar.value
           WHEN 'int' THEN ce_int.value
           WHEN 'text' THEN ce_text.value
           WHEN 'decimal' THEN ce_decimal.value
           WHEN 'datetime' THEN ce_datetime.value
           ELSE ea.backend_type
        END AS value,
        ea.is_required AS required
    FROM ", @ENTITY_TABLE, " AS ce
	LEFT JOIN eav_entity_type as eet
		ON eet.entity_table = ",'@ENTITY_TABLE',"
    LEFT JOIN eav_attribute AS ea 
        ON eet.entity_type_id = ea.entity_type_id
    LEFT JOIN ", @ENTITY_TABLE,"_varchar AS ce_varchar 
        ON ce.",@EQUAL," = ce_varchar.",@EQUAL," 
        AND ea.attribute_id = ce_varchar.attribute_id 
        AND ea.backend_type = 'varchar'
    LEFT JOIN ", @ENTITY_TABLE,"_int AS ce_int 
        ON ce.",@EQUAL," = ce_int.",@EQUAL," 
        AND ea.attribute_id = ce_int.attribute_id 
        AND ea.backend_type = 'int'
    LEFT JOIN ", @ENTITY_TABLE,"_text AS ce_text 
        ON ce.",@EQUAL," = ce_text.",@EQUAL," 
        AND ea.attribute_id = ce_text.attribute_id 
        AND ea.backend_type = 'text'
    LEFT JOIN ", @ENTITY_TABLE,"_decimal AS ce_decimal 
        ON ce.",@EQUAL," = ce_decimal.",@EQUAL," 
        AND ea.attribute_id = ce_decimal.attribute_id 
        AND ea.backend_type = 'decimal'
    LEFT JOIN ", @ENTITY_TABLE,"_datetime AS ce_datetime 
        ON ce.",@EQUAL," = ce_datetime.",@EQUAL," 
        AND ea.attribute_id = ce_datetime.attribute_id 
        AND ea.backend_type = 'datetime'
    WHERE ce.",@COLUMNID," = ", '@VALUEID', "
  ) AS tab
  WHERE tab.value != '';");
  
PREPARE entityQuery from @entityQuery;
EXECUTE entityQuery;

DEALLOCATE PREPARE entityQuery;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment