Menu

Know About Magento Object Relational Mapping (ORM)

Know About Magento Object Relational Mapping (ORM)

As We know the implementation of a "Tier Models" is a important part of an MVC framework. It represents the details of your application and handles data in applications. Magento Models play an even greater role, because they usually contain the "Business Logic".

The Magento Object Relational Mapping (ORM) has very important role in the process of working with database. Here We will go deeply to understand ORM.
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 displayed 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(200);
echo $product->getPrice();


Models in Magento are divided into three types:
1).  Models working with in-coherent data:

Here is an 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’)),
);
}
}


Above the data that works with this models is with 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.
2).  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.
3). Models working with SQL database:

This select and write data in the database through the SQL structure query
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();


Hope it will be helpful for you!

 

تعليقات

  1. Really this post cleared the concept of ORM not only in Magento but also its usability in MVC.

    Thanks.

    ردحذف

إرسال تعليق

Ads middle content1

Ads middle content2