0

I know that MAGENTO has an entity called catalog_product

When a new attribute is created there will be an entry in eav_attribute and if you check this entry there will be a column as entity_type_id

eav_attribute table also has a column known as attribute_code which is nothing but a value of Attribute Code entered while making a new attribute from admin.

I dont know which tables i need to "join" in order to obtain what i want. Query?

I am using MAGENTO 1.9 and MYSQL

Database diagram:

Image

Thanks!

0

Do you really want / need to build an SQL query? That can get complicated really fast with EAV.

To filter all products by specific attributes in Magento you can use the collections.

For example to get all Products with a name containing 'XYZ':

/** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToFilter('name', array('like' => '%XYZ%'));
$products = $collection->getItems();

http://www.beckin.com/the-official-cheat-sheet-for-magento-developers/

You can also print the Select statement of a prepared collection like this:

echo $collection->getSelect()->__toString();

The same (mostly) in sql would look like:

SELECT main.entity_id,main.sku,name.value AS name
FROM catalog_product_entity AS main 
LEFT JOIN catalog_product_entity_varchar AS name
ON main.entity_id = name.entity_id
WHERE name.attribute_id = (
    SELECT attribute_id 
    FROM eav_attribute 
    WHERE attribute_code LIKE "name"
    AND `entity_type_id` = (
        SELECT entity_type_id
        FROM eav_entity_type
        WHERE entity_type_code = 'catalog_product'
    )
)
AND name.value LIKE "%XYZ%";

EDIT: Not sure what you are exactly trying but you really should ask the other side for a proper API Endpoint (for example a rest api) instead of going directly on the database.

I tried to add some Comments for further explaining the query. Although it's mostly SQL knowledge and getting out of scope (=magento) here.

-- first the columns that should be included in the result
-- prefixed with the table alias where they come from 
-- (main = catalog_product_entity and 
-- name = catalog_product_entity_varchar restricted to the name attribute)
-- the as name in the end just renames the name.value column to name in the result
SELECT main.entity_id,main.sku,name.value AS name
-- the main product table to select the products from
FROM catalog_product_entity AS main 
-- using a left join to include all entries from the main table and exclude
-- possible values in value table without a product relation 
-- (although that should never happen anyway)
LEFT JOIN catalog_product_entity_varchar AS name
ON main.entity_id = name.entity_id
-- restricting the rows from catalog_product_entity_varchar to the name attribute
WHERE name.attribute_id = (
    -- this complete subqery just gets the attribute id for the name attribute
    SELECT attribute_id 
    FROM eav_attribute 
    WHERE attribute_code LIKE "name"
    AND `entity_type_id` = (
        -- this subquery only gets the entity_type_id for catalog_product
        SELECT entity_type_id
        FROM eav_entity_type
        WHERE entity_type_code = 'catalog_product'
    )
)
-- finally filter the result to only show products where the name contains 'XYZ'
AND name.value LIKE "%XYZ%"

EDIT2: adding two more attributes, one of type text to make it more clear

SELECT main.entity_id,main.sku,name.value AS name, description.value AS description, image.value AS image
FROM catalog_product_entity AS main 

LEFT JOIN catalog_product_entity_varchar AS name
ON main.entity_id = name.entity_id

-- description is a text attribute so you need to join catalog_product_entity_text
-- see eav_attribute table fot the attribute type
LEFT JOIN catalog_product_entity_text AS description
ON main.entity_id = description.entity_id

-- adding the image which is of type varchar (like name)
LEFT JOIN catalog_product_entity_varchar AS image
ON main.entity_id = description.entity_id

WHERE name.attribute_id = (
    SELECT attribute_id 
    FROM eav_attribute 
    WHERE attribute_code LIKE "name"
    AND `entity_type_id` = (
        SELECT entity_type_id
        FROM eav_entity_type
        WHERE entity_type_code = 'catalog_product'
    )
)

-- filter catalog_product_entity_text values for description by the attribute id of description
AND description.attribute_id = (
    SELECT attribute_id 
    FROM eav_attribute 
    WHERE attribute_code LIKE "description"
    AND `entity_type_id` = (
        SELECT entity_type_id
        FROM eav_entity_type
        WHERE entity_type_code = 'catalog_product'
    )
)

-- filter catalog_product_entity_varchar values for image by the attribute id of image
AND image.attribute_id = (
    SELECT attribute_id 
    FROM eav_attribute 
    WHERE attribute_code LIKE "image"
    AND `entity_type_id` = (
        SELECT entity_type_id
        FROM eav_entity_type
        WHERE entity_type_code = 'catalog_product'
    )
)

AND name.value LIKE "%hose%";
11
  • Thank you for replying, David. I was only given access to the database. (read-only permission). I still dont understand how did you do that query and i dont understand how could i modify it in order to get what i want :/. Can you please explain it to me? Jan 27 '19 at 17:23
  • Nice explanation!. I want to obtain a table with this fields: sku, code_brand, code_business_unit, code_line, code_group, code_business, min_sale_qty. In your query, where should i put this "fields" in order to obtain what i need?. Sorry, this is my first time using Magento and i am a little confused:( Jan 27 '19 at 17:48
  • the thing is, this is not much magento know how, mostly sql. I added two fields to so now you have 3 attribute examples. Since you mostly do not have default attributes, I cannot build the query for you (also I will not, since this platform is to educate, but not to do your job for you :P ) Jan 27 '19 at 18:10
  • Thats right!, okay. I will try to do it. Thank you for helping me! Jan 27 '19 at 18:15
  • Dude, how can i know what type these attributes are?: code_brand, code_business_unit, code_line, code_group, code_business, min_sale_qty? Jan 27 '19 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.