Entity Types in Magento
Entity Type Overview
SELECT entity_type_id, entity_type_code, entity_model, entity_table
FROM eav_entity_type;
+----------------+----------------------+-----------------------------+------------------------------+
| entity_type_id | entity_type_code | entity_model | entity_table |
+----------------+----------------------+-----------------------------+------------------------------+
| 1 | customer | customer/entity | customer_entity |
| 2 | customer_address | customer/entity | customer_address_entity |
| 3 | catalog_category | catalog/category | catalog_category_entity |
| 4 | catalog_product | catalog/product | catalog_product_entity |
| 5 | order | sales/order | sales_order |
| 7 | invoice | sales/order_invoice | sales_invoice |
| 8 | creditmemo | sales/order_creditmemo | sales_creditmemo |
| 9 | shipment | sales/order_shipment | sales_shipment |
+----------------+----------------------+-----------------------------+------------------------------+
Entity Type Configuration
<!-- Custom entity type in eav_entity_type.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Eav/etc/eav_entity_type.xsd">
<entity type="vendor_custom"
label="Custom Entity"
entity_model="Vendor\Custom\Model\Entity"
entity_table="vendor_custom_entity"
value_table_prefix="vendor_custom_entity"/>
</config>
Entity IDs and Loading
Entity ID Structure
-- Product entity ID
SELECT entity_id, sku, type_id, attribute_set_id
FROM catalog_product_entity
WHERE entity_id = 123;
+-----------+----------+-----------+------------------+
| entity_id | sku | type_id | attribute_set_id |
+-----------+----------+-----------+------------------+
| 123 | TSH-001 | simple | 4 |
+-----------+----------+-----------+------------------+
-- Category entity ID
SELECT entity_id, parent_id, path, level
FROM catalog_category_entity
WHERE entity_id = 45;
+-----------+-----------+---------+-------+
| entity_id | parent_id | path | level |
+-----------+-----------+---------+-------+
| 45 | 2 | 1/2/45 | 2 |
+-----------+-----------+---------+-------+
Loading Entities in PHP
// Using repositories (recommended - inject via constructor)
class EntityLoader
{
public function __construct(
private \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
private \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
private \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {}
public function loadProduct(int $productId): void
{
$product = $this->productRepository->getById($productId);
echo $product->getName(); // Loads from EAV value tables
echo $product->getPrice(); // Loads from decimal value table
echo $product->getSku(); // From varchar value table
}
public function loadBySku(string $sku): void
{
$product = $this->productRepository->get($sku);
}
public function loadCategory(int $categoryId): void
{
$category = $this->categoryRepository->get($categoryId);
}
public function loadCustomer(int $customerId): void
{
$customer = $this->customerRepository->getById($customerId);
}
}
Entity Data Array
// Get all entity data
$product->getData(); // Returns array of all attributes
// Get specific attribute
$product->getData('name'); // Returns attribute value
$product->getAttributeCode(); // Returns attribute code
// Check if attribute exists
$product->hasData('custom_attribute');
Entity Management
Saving Entities
// Using factories (inject via constructor)
class ProductManager
{
public function __construct(
private \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
private \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
private \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {}
public function createProduct(): void
{
// Create via repository (handles factory internally)
$product = $this->productRepository->create();
$product->setName('New Product')
->setSku('NEW-001')
->setPrice(29.99)
->setStatus(1)
->setAttributeSetId(4)
->setTypeId('simple');
$this->productRepository->save($product);
}
public function updateProduct(int $productId): void
{
$product = $this->productRepository->getById($productId);
$product->setName('Updated Name');
$this->productRepository->save($product);
}
}
Deleting Entities
// Using repositories
public function deleteProduct(int $productId): void
{
$product = $this->productRepository->getById($productId);
$this->productRepository->delete($product);
}
public function deleteCategory(int $categoryId): void
{
$category = $this->categoryRepository->get($categoryId);
$this->categoryRepository->delete($category);
}
Entity Collection
// Using collection factory (inject via constructor)
class ProductLister
{
public function __construct(
private \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
) {}
public function listProducts(): void
{
$collection = $this->collectionFactory->create();
$collection->addAttributeToSelect(['name', 'price', 'sku'])
->addFieldToFilter('status', 1)
->setPageSize(20)
->setCurPage(1);
foreach ($collection as $product) {
echo $product->getName();
}
$count = $collection->count();
$ids = $collection->getAllIds();
}
}
Entity Type Specifics
Product Entity Specifics
// Product has type_id determining behavior
$product->getTypeId(); // simple, configurable, grouped, bundle, virtual, downloadable
// Product has attribute_set_id
$product->getAttributeSetId(); // Links to attribute set
// Product SKU is unique
$product->getSku(); // Must be unique across all products
Category Entity Specifics
// Category has hierarchy
$category->getParentId(); // Parent category ID
$category->getPath(); // Full path: 1/2/3/45
CATEGORY->getLevel(); // Depth level
$category->getChildrenCount(); // Number of children
// Category products
$category->getProductCollection(); // Products in category
Customer Entity Specifics
// Customer has addresses
$customer->getPrimaryBillingAddress();
$customer->getPrimaryShippingAddress();
$customer->getAddresses(); // All addresses
// Customer has group
$customer->getGroupId(); // Customer group ID
// Customer auth
$customer->authenticate($password);
$customer->changePassword($oldPassword, $newPassword);
Order Entity Specifics
// Order uses flat structure (not EAV)
$order->getState(); // new, processing, complete, closed
$order->getStatus(); // pending, processing, shipped
$order->getItems(); // Order items
$order->getPayment(); // Payment info
$order->getShippingAddress();
$order->getBillingAddress();
// Order is immutable after creation
// Status changes create history records
Quiz
1. What is entity_type_id 4 in Magento?
2. How do you load a product by SKU?
3. What determines a product's behavior type?
Flashcards
Question
What are Magento's EAV entity types?
Click to reveal answer
Answer
customer, customer_address, catalog_category, catalog_product, order, invoice, creditmemo, shipment
Question
How to load entity by ID?
Click to reveal answer
Answer
$object->load($id) or $repository->getById($id)
Question
What is entity_type_id for products?
Click to reveal answer
Answer
4 (catalog_product)
Question
What does type_id determine?
Click to reveal answer
Answer
Product behavior: simple, configurable, grouped, bundle, virtual, downloadable
Question
How to get all entity data?
Click to reveal answer
Answer
$entity->getData() returns array of all attributes
Revision Notes
Key Takeaways
- 1. Magento has 8 main EAV entity types: customer, address, category, product, order, invoice, creditmemo, shipment
- 2. Entity types map to models and tables in eav_entity_type
- 3. Load entities with load($id) or repository->getById($id)
- 4. Save entities with save() after setting data
- 5. Product type_id determines behavior (simple, configurable, etc.)
Interview Tips
- • Explain the difference between entity types
- • Discuss how entity loading works with EAV
- • Describe product type differences
Cheat Sheet
Entity Types:
1 = customer
2 = customer_address
3 = catalog_category
4 = catalog_product
5 = order
7 = invoice
8 = creditmemo
9 = shipment
Loading:
$product->load($id)
$repository->getById($id)
$repository->get($sku)
Saving:
$entity->setData($key, $value)->save()
$entity->save()