string(45) "/2010/07/12/magento-diagram-entity-attribute/"
Home » Magento Diagram

Magento Diagram – Entity / Attribute

12 July 2010 1,884 views One Comment

Magento Diagram – Entity / Attribute (Magento 1.4.1.0)

Magento Diagram - Entity / Attribute

Entity Type / Entity Code

This query outputs all the available Magento Entities.

mysql> SELECT entity_type_id,entity_type_code FROM `eav_entity_type`;
+----------------+------------------+
| entity_type_id | entity_type_code |
+----------------+------------------+
|              3 | catalog_category |
|              4 | catalog_product  |
|              7 | creditmemo       |
|              1 | customer         |
|              2 | customer_address |
|              6 | invoice          |
|              5 | order            |
|              8 | shipment         |
+----------------+------------------+
8 rows in set (0.00 sec)

Entity Code / Entity Model

This query outputs the Entity Model associated to each Entity Type Code.

mysql> SELECT entity_type_code,entity_model FROM `eav_entity_type`;
+------------------+------------------------+
| entity_type_code | entity_model           |
+------------------+------------------------+
| customer         | customer/customer      |
| customer_address | customer/address       |
| catalog_category | catalog/category       |
| catalog_product  | catalog/product        |
| order            | sales/order            |
| invoice          | sales/order_invoice    |
| creditmemo       | sales/order_creditmemo |
| shipment         | sales/order_shipment   |
+------------------+------------------------+
8 rows in set (0.01 sec)

Entity Code/ Attribute Model

Two entities, catalog_category and catalog_product, share an Attribute Model identified by : catalog/resource_eav_attribute.

mysql> SELECT entity_type_code,attribute_model FROM `eav_entity_type`;
+------------------+--------------------------------+
| entity_type_code | attribute_model                |
+------------------+--------------------------------+
| customer         |                                |
| customer_address |                                |
| catalog_category | catalog/resource_eav_attribute |
| catalog_product  | catalog/resource_eav_attribute |
| order            |                                |
| invoice          |                                |
| creditmemo       |                                |
| shipment         |                                |
+------------------+--------------------------------+
8 rows in set (0.00 sec)

How to fetch the Entity Type Code from the Entity Type Id ?

With a PHP script (version 1):


<?php
require_once('app/Mage.php');
Mage::app();

// entity type code: catalog_product
$entityTypeId = Mage::getModel('eav/entity')
                      ->setType('catalog_product')
                      ->getTypeId();
var_dump($entityTypeId);

// entity type code: catalog_category
$entityTypeId = Mage::getModel('eav/entity')
                      ->setType('catalog_category')
                      ->getTypeId();
var_dump($entityTypeId);

// etc... (works for all entity type code)
?>

With SQL queries :


#
# catalog_product
#
SQL query executed on MySQL server :
SELECT `eav_entity_type`.* FROM `eav_entity_type` WHERE (eav_entity_type.entity_type_code='catalog_product');

#
# catalog_category
#
SQL query executed on MySQL server :
SELECT `eav_entity_type`.* FROM `eav_entity_type` WHERE (eav_entity_type.entity_type_code='catalog_category');

With a PHP script (version 2):
Entity type code may also be fetched from a limited number of models.

<?php
require_once('app/Mage.php');
Mage::app();

// model: catalog/product
$entityTypeId  = Mage::getModel('catalog/product')->getResource()->getTypeId();
var_dump($entityTypeId );

// model: catalog/category
$entityTypeId  = Mage::getModel('catalog/category')->getResource()->getTypeId();
var_dump($entityTypeId );

// model: customer/customer
$entityTypeId  = Mage::getModel('customer/customer')->getResource()->getTypeId();
var_dump($entityTypeId );

// model: sales/order
$entityTypeId  = Mage::getModel('customer/address')->getResource()->getTypeId();
var_dump($entityTypeId);
?>

How to fetch the attributes associated to an entity ?

With a PHP script :

<?php
require_once('app/Mage.php');
Mage::app();

$collection = Mage::getResourceModel('catalog/product_attribute_collection')
              ->addVisibleFilter();

$collection->load();

foreach ($collection as $item) {
    printf("%s %s\n",
      $item->getData('entity_type_id'),
      $item->getData('attribute_code')
    );
}

/*
OUTPUT :

4 color
4 cost
4 custom_design
4 custom_design_from
4 custom_design_to
4 custom_layout_update
4 description
4 enable_googlecheckout
4 gallery
4 gift_message_available
4 image
4 is_recurring
4 manufacturer
4 media_gallery
4 meta_description
4 meta_keyword
4 meta_title
4 name
4 news_from_date
4 news_to_date
4 options_container
4 page_layout
4 price
4 price_view
4 recurring_profile
4 short_description
4 sku
4 small_image
4 special_from_date
4 special_price
4 special_to_date
4 status
4 tax_class_id
4 thumbnail
4 tier_price
4 url_key
4 visibility
4 weight

*/
?>

With SQL query :

SELECT `main_table`.*, `additional_table`.*
  FROM `eav_attribute` AS `main_table`
  INNER JOIN `catalog_eav_attribute` AS `additional_table`
  ON additional_table.attribute_id=main_table.attribute_id
  WHERE (main_table.entity_type_id=4)
  AND (additional_table.is_visible=1);
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

One Comment »

  • Daniel said:

    great post, thanks for sharing