Magento Interview Question Part-2

Some more questions to go:-

1. What is EAV model? How Magento uses EAV Model?

EAV basically stands for Entity Attribute Value. I will be explaining it with an example in terms of Magento, considering the product as an Entity. We can have a various attribute for a product i.e, various column in which product related data will be stored, for example, color, product name, size, product description, length, weight etc. Now Magento manages lots of product and it is always a possibility that each type of product might not require all columns to have a value such as, if we are selling a banana then length is not needed for that, So the whole column will always be empty which will simply make our space as non-utilized. Also if we have 1000 types of product and then in total they will be using lots of columns and thus make our table very huge. So in EAV, we break the things and attribute/column are maintained as a row. Here we are mainly normalizing our tables.
Now as per Magento we have table eav_entity, which list all entity type. Eav_Attribute which has attribute/column data. And further on basis of entity and their datatype we have tables which save their value.
Thus, in short, The most widely used way to store entities is implementing attributes as columns and entities as rows.

2. List the different type of product that Magento has and mentioned when to use which one?

Simple Product:-
It is the most basic type of physical product. For example:-
I want to sell a shirt with Small size.Each simple product has a unique SKU.

Configurable Product:-
In these we provide a different option for a simple product, this allows you to select any one of those options and order. For example:-
We have a configurable product, shirt with different size such as s, m, xs, xl and color white, black, gray. This as a whole is configurable and, in admin, they all are the different simple product such as a shirt with size m and a color black. The configurable product basically ties them all together and gives the end user the choice, usually from a drop-down menu to select whichever product he wants.

Grouped Product:-
It is a “group” of simple products so that combo or several associated products can be sold as a unit. For example, let’s say we want to sell the camera lens + a memory card + a camera. Each of those items would be simple products; we want to sell them as a group so that we can sell them in an offer.

Bundled Product:-
Simple to what group product was doing but group product does give any choice to end user to select. Thus we have bundled product, for example, we can have a memory card with different warranty or of a different company. Now in bundle, we can allow a customer to choose any one of the options with the product.

Virtual Product:-
These are not the physical product, they are sort of services or anything which cannot be touched and feel. Such as cable repairing.

Downloadable product:-
A video, images anything that you can download are included in these product type.

3. What is ORM and how does it work in Magento?

ORM stands for Object Relational Mapping, it is a programming technique for converting data into objects and vice-versa. In Magento, it is implemented as a model, as all Model inherits Varien_Object class and heavily used PHP getter and setter functions to retrieve data objects.
For example:-
$product = Mage::getModel(‘catalog/product’)->setPrice(100);
echo $product->getPrice();

Above we are setting the price as 100 for product but model is not directly interacting with the database it is rather dealing with objects, not with data. Thus if we are using ORM then developer with set and get values through objects they will not be directly interacting with the database.

4. Does Magento follow Master-slave architecture, if yes then tell us how do we create that in Magento?

Yes, Magento support master-slave architecture. It is capable of splitting off reads/writes to different database servers mainly to a master and to a slave which allow us to route read request to additional servers and forwarding all the update/write queries to a single master. Let us see now how we can configure master-slave with Magento:-
In your app/etc/local.xml add a node for default_read and pass the slave database details:-

<default_setup>
    <connection>
        <host><![CDATA[Master-host]]></host>
        <username><![CDATA[user]]></username>
        <password><![CDATA[pass]]></password>
        <dbname><![CDATA[magentodb]]></dbname>
        <active>1</active>
    </connection>
</default_setup>
<default_read>
    <connection>
        <use/>
        <host><![CDATA[Slave-host]]></host>
        <username><![CDATA[user]]></username>
        <password><![CDATA[pass]]></password>
        <dbname><![CDATA[magento]]></dbname>
        <type>pdo_mysql</type>
        <model>mysql4</model>
        <initStatements>SET NAMES utf8</initStatements>
        <active>1</active>
    </connection>
</default_read> 

Secondly, you need to modify MySQL configuration file, make sure we do it before changing local.xml
Configuration for master server
edit /etc/my.cnf and add below content in the file:

[mysqld]
server-id       = 1
log_bin         = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size     = 100M
binlog_do_db        = magento_demo
binlog_ignore_db    = mysql 

then restart your mysql server. Configuration for slave server edit /etc/my.cnf and add below content in the file:

[mysqld]
server-id=2
log-bin=mysql-bin
master-host=192.168.1.2
master-user=username
master-password=111111
master-port=3306
replicate-do-db=magento_demo
replicate-ignore-db=mysql
master-connect-retry=60 

then restart your mysql server.

Above changes will make your architecture ready.

5. What is the directory structure of Magento?

Below are the main files and folders that you should remember:

app (directory) – This folder contains the modules, themes, configuration and translation files. cron.php – a Cron Job should be set for this file.
downloader (directory) – This is the storage of the web downloader files. It is mainly used for installation and upgrade of Magento through your browser;
index.php – the main index file for Magento;
js (directory) – Contains the pre-compiled libraries of the JavaScript code included in Magento;
lib (directory) – The Magento core code is located in this folder. It contains the PHP libraries;
media (directory) – This stores Magento media files – images, thumbnails, uploaded products images.
skin (directory) – This directory contains – images, JavaScript files, CSS files, Flash files.
var (directory) – Cache, sessions, database backups, data exports and cached error reports can be found in this directory;

6. What is the difference between cron.php and cron.sh?

Cron.php is a PHP script and cron.sh is a shell script, cron.sh internally calls cron.php itself. We set Cron file on the server in Cron jobs when we schedule cron. If PHP variable id not set we can directly use cron.sh.

7. How Cron jobs are scheduled in Magento and what is the workflow of Cron in Magento? In which table did we manage Cron schedule?

Cron is a daemon that executes automatically at scheduled time. Sometimes we have requirements in our applications that needs to be automated such as indexing, backup of various things cannot be always taken care manually on daily basis. Thus we write scripts and set Cron them. Magento itself provide a way to configure or schedule a Cron job. Magento uses the cron.php file, which can be found in the root of your application to manage and dispatch cronjobs. The cron.php script internally executes Magento cronjob manager. It is highly recommended to set Cron job using cron.sh rather than cron.php.Cron_Schedule table manages all Cron schedule.

8. What is compilation in Magento? How does compilation improve performance?

Magento Compilation allows you to compile all Magento installation files in order to create a single include path. since all the class files are included and found at the same path it improves the class loading time and thus the performance of your online store is supposed to give between 25% and 50% increase in your page load speed. During development phase we keep compilation off, else we need to run compilation again and again. To disable Magento Compilation, go to your Admin Panel -> System -> Tools -> Compilation and click on the Disable button.
When we run compilation process Magento copies all your class, model, helper, blocks, and lib files to “includes/src” folder. We can check that by opening “includes/src” folder, there you will find the class directly inside a single folder. There will be files with the name as follows : – Mage_Catalog_Block_Breadcrum.php, Varien_Profile.php etc directly in a single folder itself.

In index.php below code is executed, which include file that define compilation path

$compilerConfig = MAGENTO_ROOT . '/includes/config.php';
if (file_exists($compilerConfig)) {
    include $compilerConfig;
}

Here is the /includes/conig.php file code that is included in above code:-

#define('COMPILER_INCLUDE_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR.'src');

Now in Mage.php you can see that if compilation path is set than class are directly loaded from include/src folder

if (defined('COMPILER_INCLUDE_PATH')) {
    $appPath = COMPILER_INCLUDE_PATH;
    set_include_path($appPath . PS . Mage::registry('original_include_path'));
    include_once COMPILER_INCLUDE_PATH . DS . "Mage_Core_functions.php";
    include_once COMPILER_INCLUDE_PATH . DS . "Varien_Autoload.php";
} else {
    /**
     * Set include path
     */
    $paths = array();
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
    $paths[] = BP . DS . 'lib';
 
    $appPath = implode(PS, $paths);
    set_include_path($appPath . PS . Mage::registry('original_include_path'));
    include_once "Mage/Core/functions.php";
    include_once "Varien/Autoload.php";
}

9. What is the difference between resource model, collection, and model?

Model in Magento handles logical operation with the database. There are several kinds of model like
ActiveRecord/one-object-one-table Model, an Entity Attribute Value (EAV) Model. Each Model also gets a Model Collection.
A Magento model object contains a reference to a resource model, which interacts with the database on behalf of the “model”. It is the one who performs CRUD operation on the database.
Collection model contains code that fetched a group of model objects. On collection, we implement filtration and sorting kinds of features. Collection objects inherit from a different chain of classes than the normal resource models, It is also considered a resource model and is instantiated with the Mage::getResourceModel method.

10. What is the difference between a flat table and EAV model?

Though we have covered this in the first question of this page also but let’s discuss more of it. EAV is an entity attribute value database model, where data is fully in normalized form. In Eav model, each data value is stored in their respective data type table. Example, for a product, product ID is stored in catalog_product_entity_int table, product name in catalog_product_entity_varchar, product price in catalog_product_entity_decimal, product created date in catalog_product_entity_datetime and product description in catalog_product_entity_text table. EAV model is complex and thus slow as it joins several tables to retrieve even a simple information.

A flat model uses just one table, so it’s not normalized and thus it uses more database space. It is faster than EAV in retrieving information but it is not good for dynamic requirements where you may have to add more columns in a database table in future. It’s good when comes to performance, as it will only require one query to load whole product. Columns are called fields in flat model. Magento uses indexing process to manage data in flat tables for products so that frontend can be speed up.

1 Comments

  1. I think this is one of the most vital information for me.
    And i’m glad reading your article. But should remark on some general things, The website style
    is great, the articles is really excellent : D. Good job, cheers

    Reply

Leave a Comment.