Explain Magento’s MVC architecture
MVC stands for Model-View-Controller.Any application that separates its data access, business logic and user interface is called MVC.
There can be two types of MVC: convention-based and configuration-based.
1. CakePHP is convention-based, i.e. you just need to follow the instructions of the core system to get your module ready in just few lines.
2. Magento is configuration-based, i.e. you need to specify each and every thing to your module’s config file in order to get it work. Magento has Controller (for Routing), Block (for Business Logic), Model (for DB access, sql) and Template file (for Presentation i.e. View).
How Magento’s MVC works:
1. When you enter the URL
this URL is intercepted by one PHP file called index.php which instantiates Magento application.
2. Magento application instantiates Front Controller object
3. Further, front controller instantiates Router objects (specified in module’s config.xml, global tag)
4. Now, Router is responsible to “match” the frontname which is in our URL
5. If “match” is found, it sees controller name and method name in the URL, which is finally called.
6. Now depending on what is written in action name (method name), it is executed. If any models are called in it, the controller method will instantiate that model and call the method in it which is requested.
7. Then the controller action (method) instantiate the Layout object, which calls Block specified for this action (method) name (Each controller action name have block and template file associated with it, which can be found at app/design/frontend or adminhtml/namespace/module/layout/module.xml file, name of layout file (module.xml) can be found in config.xml of that module, in layout updates tag).
8. Template file (.phtml) now calls the corresponding block for any method request. So, if you write $this->methodName in .phtml file, it will check “methodName” in the block file which is associated in module.xml file.
9. Block contains PHP logic. It references Models for any data from DB.
10. If either Block, Template file or Controller need to get/set some data from/to database, they can call Model directly like Mage::getModel(‘modulename/modelname’).
How Magento ORM works?
ORM stands for Object Relational Mapping.
It’s a programming technique used to convert different types of data to Objects and vice versa.
In Magento, ORM is shown as Model (based on Zend Framework’s Zend_Db_Adapter), which is further breaks down to two types of Models.
– First is the “simple” i.e. Regular Models which is nothing but flat table or our regular table structure.
– Second Model is EAV (Entity Attribute Value), which is quite complicated and expensive to query.
All Magento Models interacting with database are inherited from Mage_Core_Model_Abstract class, which is further inherited from Varien_Object.
Difference between two Models is,
Simple Model is inherited from Mage_Core_Model_Resource_Db_Abstract class.
while EAV is inherited from Mage_Eav_Model_Entity_Abstract.
Where 1 is the primary key id for some Regular/Simple table, while in EAV so many tables are joined to fetch just single row of data.
What is EAV in Magento?
EAV stands for Entity Attribute Value.
It is a technique which allows you to add unlimited columns to your table virtually. Means, the fields which is represented in “column” way in a regular table, is represented in a “row” (records) way in EAV.
In EAV, you have one table which holds all the “attribute” (table field names) data, and other tables which hold the “entity” (id or primary id) and value (value for that id) against each attribute.
In Magento, there is one table to hold attribute values called eav_attribute and 5-6 tables which holds entity and data in fully normalized form,
– eav_entity, eav_entity_int (for holding Integer values),
– eav_entity_varchar (for holding Varchar values),
– eav_entity_datetime (for holding Datetime values),
– eav_entity_decimal (for holding Decimal/float values),
– eav_entity_text (for holding text (mysql Text type) values).
EAV is expensive and should only be used when you are not sure about number of fields in a table which can vary in future. To just get one single record, Magento joins 4-5 tables to get data in EAV. But this doesn’t mean that EAV only has drawbacks.
The main advantage of EAV is when you may want to add table field in future, when there are thousands or millions of records already present in your table. In regular table, if you add table field with these amount of data, it will screw up your table, as for each empty row also some bytes will be allocated as per data type you select. While in EAV, adding the table column will not affect the previously saved records (also the extra space will not get allocated!) and all the new records will seamlessly have data in these columns without any problem.
Difference between Mage::getSingleton() and Mage::getModel()
The difference between Mage:getSingleton() and Mage::getModel() is that
Mage:getSingleton() does not create an object if the object for same class is already created, While Mage::getModel() creates new objects every time for the class when it’s called.
Mage::getSingleton() uses the “singleton design pattern” of PHP. If the object is not created, it will create it.
Mage::getSingleton() is mostly used when you want to create an object once, modify it and later fetch from it.
Popular example is session, you first create a session object, and then add/remove values from session across different pages, so that it retains your values (e.g. cart values, logged in customer details, etc.) and doesn’t create new session object losing your last changes.
Mage::getModel() is used when you want to have the fresh data from the database. Example is when you want to show records from database.