VOOZH about

URL: https://deepwiki.com/MahoCommerce/maho/4.2-models-and-resource-models

⇱ Models and Resource Models | MahoCommerce/maho | DeepWiki


Loading...
Last indexed: 15 May 2026 (ea8ab8)
Menu

Models and Resource Models

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.

Architecture Overview

The model layer follows a three-tier architecture that separates business logic from database operations:

Diagram: Model Layer Architecture


Key Components

ComponentBase ClassPrimary MethodsPurpose
ModelMage_Core_Model_Abstractload(), save(), delete(), _beforeSave(), _afterSave()Business logic, data validation, event dispatching.
Resource ModelMage_Core_Model_Resource_Db_Abstractload(), save(), delete(), _getReadAdapter(), _getWriteAdapter()Database CRUD operations, table management.
CollectionMage_Core_Model_Resource_Db_Collection_AbstractaddFieldToFilter(), setOrder(), load()Query multiple entities with filtering/sorting.
AdapterMaho\Db\Adapter\AdapterInterfaceselect(), insert(), update(), delete(), fetchAll()Database connection and query execution.
Query BuilderMaho\Db\Selectfrom(), 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

Model Layer

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

Model Lifecycle

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

Collections provide a fluent interface for querying multiple entities. They manage a Maho\Db\Select object and iterate over the results to instantiate models.

Product Collections

The Mage_Catalog_Model_Resource_Product_Collection is one of the most complex collections in the system. It handles:

Diagram: 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

Abstract Rule Models

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

FeatureClassDescription
Condition AggregatorMage_Rule_Model_Condition_CombineHandles "ALL" or "ANY" logic for nested conditions app/code/core/Mage/Rule/Model/Condition/Combine.php49-50
SQL GenerationprepareConditionSql()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

URL Rewrites and Model Integration

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

Sources: 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