Magento Object Relational Mapping (ORM) plays a vital role in the process of working with database. So I have now extended the tutorial to help you more deeply understand ORM. Please keep following this reference for your using of Magento.

1. What is ORM

Object Relational Mapping (ORM) is a programming technique for converting between types of data and objects in OOP. There are 2 types of ORM:
  • Convert different types of data to objects
  • Convert objects to various types of data

2. ORM in Magento

In Magento, ORM is shown as Models (in Magento design pattern MVC). Most of the models are inherited from the Varien_Object class, along with using PHP magic _get and _set functions to set and retrieve the data of the object:

$product = Mage::getModel(‘catalog/product’)->setPrice(100);
echo $product->getPrice();

Models in Magento are divided into 3 types:

  • Models working with incoherent data: The typical example for this type is adminhtml/system_config_source_yesno model with the content below:
class Mage_Adminhtml_Model_System_Config_Source_Yesno
{
    public function toOptionArray(){
        return array(
            array('value' => 1, 'label'=>Mage::helper('adminhtml')->__('Yes')),
            array('value' => 0, 'label'=>Mage::helper('adminhtml')->__('No')),
        );
    }
}

Data that this type of models works with is 0/1 value and Yes/No label. The model doesn’t get data from the database and doesn’t write data in the database as well.

  • Models working with XML database: such as core/config model. This model works with XML files which are configuration files in Magento. The loading data function of the model is as follows:
public function loadBase(){
        $etcDir = $this->getOptions()->getEtcDir();
        $files = glob($etcDir.DS.'*.xml');
        $this->loadFile(current($files));
        while ($file = next($files)) {
            $merge = clone $this->_prototype;
            $merge->loadFile($file);
            $this->extend($merge);
        }
        if (in_array($etcDir.DS.'local.xml', $files)) {
            $this->_isLocalConfigLoaded = true;
        }
        return $this;
    }

The model working with XML database converts the data stored by XML configuration file to working objects. There are separated methods to work with theXML database like getNode, getSectionNode…

  • Models working with SQL database: take and write data in the database through the SQL structure query demand. The responsibility of these models is to change actions to the data query demands.

• Models working with one database table (such as core/website model). This model works with one table in database. Loading or saving the data will just relate to this table. For instance:

$website = Mage::getModel(‘core/website’)->load(0);
$website->setId(1)->save();

• Models working with multi – database table: work with EVA database. For example: catalog/product model. In this case, loading or saving data will be relevant to a set of table. This model has to map the data of multi-table to its object. The data query will be implemented during using this model.

$product = Mage::getModel(‘catalog/product’)->load(1);
$product->setId(2)->save();

That’s all for today. I hope you’ve enjoyed my tutorial and find it helpful. See you again in the next posts.

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

1 Comment

  1. Claudiu Marginean Reply

    There are more types of model in Magento.
    Session and cookies are models that store the date in the $_SESSION or $_COOKIE variables and based on the configuration the session is stored on in files or in database.

Write A Comment