![]() |
VOOZH | about |
This page documents the Model and Resource Model architecture in Maho, which implements a data access layer that separates business logic from database operations. Models contain business logic and validation, while Resource Models handle all database interactions through database adapters.
For information about the EAV (Entity-Attribute-Value) system used by catalog entities, see EAV System. For information on the database abstraction layer and multi-database support, see Multi-Database Support.
The model layer follows a three-tier architecture that separates business logic from database operations:
Diagram: Model Layer Architecture
| Component | Base Class | Primary Methods | Purpose |
|---|---|---|---|
| Model | Mage_Core_Model_Abstract | load(), save(), delete(), _beforeSave(), _afterSave() | Business logic, data validation, event dispatching. |
| Resource Model | Mage_Core_Model_Resource_Db_Abstract | load(), save(), delete(), _getReadAdapter(), _getWriteAdapter() | Database CRUD operations, table management. |
| Collection | Mage_Core_Model_Resource_Db_Collection_Abstract | addFieldToFilter(), setOrder(), load() | Query multiple entities with filtering/sorting. |
| Adapter | Maho\Db\Adapter\AdapterInterface | select(), insert(), update(), delete(), fetchAll() | Database connection and query execution. |
| Query Builder | Maho\Db\Select | from(), where(), join(), order(), limit() | Fluent interface for building SQL queries. |
Sources: Mage_Catalog_Model_Resource_Product_Collection app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php17-18 Mage_Eav_Model_Entity_Abstract app/code/core/Mage/Eav/Model/Entity/Abstract.php13-14 Mage_Core_Model_Abstract app/code/core/Mage/Core/Model/Abstract.php22-23
Models encapsulate business logic and delegate database operations to resource models. They never directly interact with the database. In Maho, models often extend Mage_Core_Model_Abstract app/code/core/Mage/Core/Model/Abstract.php22 or \Maho\DataObject app/code/core/Mage/Core/Model/Abstract.php22
Models follow a standard lifecycle with protected hook methods for custom logic. Each CRUD operation has before/after hooks at both the model and resource model level.
Diagram: Model CRUD Lifecycle with Hooks
Sources: Mage_Catalog_Model_Resource_Eav_Attribute::_beforeSave app/code/core/Mage/Catalog/Model/Resource/Eav/Attribute.php94-118 Mage_Catalog_Model_Resource_Eav_Attribute::_afterSave app/code/core/Mage/Catalog/Model/Resource/Eav/Attribute.php121-129 Mage_Core_Model_Abstract::_init app/code/core/Mage/Core/Model/Abstract.php98-101
Collections provide a fluent interface for querying multiple entities. They manage a Maho\Db\Select object and iterate over the results to instantiate models.
The Mage_Catalog_Model_Resource_Product_Collection is one of the most complex collections in the system. It handles:
$_flatEnabled cache app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php34price_index table to allow filtering and sorting by final price app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php133-140_addMinimalPrice or _addTaxPercents app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php69-90catalog_prepare_price_select to allow external modification of price calculations app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php245Diagram: Collection to Database Mapping
Sources: Mage_Catalog_Model_Resource_Product_Collection app/code/core/Mage/Catalog/Model/Resource/Product_Collection.php22-28 Mage_Catalog_Model_Resource_Product_Collection::_map app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php133-140 Mage_Catalog_Model_Resource_Product_Collection::_preparePriceExpressionParameters app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php225-253
Maho utilizes complex model structures for conditional logic, such as in Catalog Price Rules or Customer Segmentation. The Mage_Rule_Model_Condition_Combine class manages recursive condition logic app/code/core/Mage/Rule/Model/Condition/Combine.php23
| Feature | Class | Description |
|---|---|---|
| Condition Aggregator | Mage_Rule_Model_Condition_Combine | Handles "ALL" or "ANY" logic for nested conditions app/code/core/Mage/Rule/Model/Condition/Combine.php49-50 |
| SQL Generation | prepareConditionSql() | Converts complex rule objects into valid SQL WHERE clauses app/code/core/Mage/Rule/Model/Condition/Combine.php38-51 |
| Instance Caching | _getNewConditionModelInstance() | Optimizes performance by caching condition model instances app/code/core/Mage/Rule/Model/Condition/Combine.php62-80 |
Sources: Mage_Rule_Model_Condition_Combine app/code/core/Mage/Rule/Model/Condition/Combine.php38-51 Mage_Rule_Model_Condition_Combine::_getNewConditionModelInstance app/code/core/Mage/Rule/Model/Condition/Combine.php62-80 Mage_Rule_Model_Condition_Abstract app/code/core/Mage/Rule/Model/Condition/Abstract.php43
Models like Mage_Catalog_Model_Url act as service layers that coordinate between resource models and URL rewrite logic to generate SEO-friendly paths app/code/core/Mage/Catalog/Model/Url.php13-14
getStoreRootCategory to determine the base for URL generation app/code/core/Mage/Catalog/Model/Url.php203-215refreshRewrites method handles bulk URL generation for specific stores app/code/core/Mage/Catalog/Model/Url.php248-259Sources: Mage_Catalog_Model_Url::getStoreRootCategory app/code/core/Mage/Catalog/Model/Url.php203-215 Mage_Catalog_Model_Url::refreshRewrites app/code/core/Mage/Catalog/Model/Url.php248-259