Magento Functions

       To Get controller, action, module, route information

1  >  Get controller name

Mage::app()->getRequest()->getControllerName();

2 >  Get action name

Mage::app()->getRequest()->getActionName();

3 > Get route name

Mage::app()->getRequest()->getRouteName();

4 > Get module name

Mage::app()->getRequest()->getModuleName();

       Other functions

1 > Get the path of your magento page

 $this->getUrl('mypage')

2 > . Get Image url of current category

 $this->getCurrentCategory()->getImageUrl();

3 >  Check whether the current category is Top category

 $this->IsTopCategory();

4 >  Get description of current category.

$this->getCurrentCategory()->getDescription();

5 >  Get current store id.

    $storeId = Mage::app()->getStore()->getId();

6> Get current store name.

    $storeName = Mage::app()->getStore()->getName();

7 > Get current store code.

     $storeCode = Mage::app()->getStore()->getCode();

8 > Get website name.

     $websiteName = Mage::app()->getWebsite()->getName();

9 >  Get session id.

     $sessionId = Mage::getModel('core/session')->getSessionId();

10 > Get customer id.

$customerId = Mage::getModel('customer/session')->getCustomerId();

11 >  Get guest id.

         $vistitorId = Mage::getModel('core/session')->getVisitorId();

Question and Answers(part-1)

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

http://mysite.com/frontname/controller/method/param1/value1/param2/value2″ ,

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.

Mage::getModel(‘module/model’)->load(1);

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.

Question and Answers(part-2)

Q 1. What is benefit of namespace (package) in magento?

Ans. We can have more than one module with same name but they should be
 placed in different namespaces. 
All magento core modules are contained in mage namespace.
core/Mage/Catalog
local/Muster/CustomModule

Q 2. What is the difference between Mage::getSingletone() andMage::getModel() in Magento

Ans. Mage::getSingletone() always finds for an existing object if not 
then create that a newobject
 but Mage::getModel() always creates a new object.

Q 3. How to include CMS block in template file(.phtml)?

Ans. Access block's content from .phtml template file by :
echo $this->getLayout()->createBlock('cms/block')
->setBlockId('static_block_id')->toHTML();

Q 4. Why Magento use EAV database model ?

Ans. In EAV database model, 
data are stored in different smaller tables
rather than storing in a single table.
Example:Product name is stored in catalog_product_entity_varchar table.
 Product id is stored in catalog_product_entity_int table.
 Product price is stored in catalog_product_entity_decimal table.
Magento Use EAV database model for easy upgrade 
and development as this model gives more flexibility 
to play with data and attributes.

Q 5. How to add an external javascript/css file to Magento

Ans.
<action method="addJs"><script>js/yourfile.js</script></action>
 <action method="addCss"><stylesheet>css/yourstyle.css</stylesheet>
</action>
 or
 <action method="addItem"><type>skin_js</type><name>js/ yourfile.js</name>
</action>
 <action method="addItem">
<type>skin_css</type><name>css/yourstyle. css</name>
</action>

Q 6. What technology used by Magento?

Ans. Magento uses PHP as a web server scripting language 
and the MySQL Database. 
The data model is based on the Entity-attribute-value(EAV) model 
that stores data objects in tree structures, 
thus allowing a change to a data structure 
without changing the database definition.

Q 7. What are handles in magento (layout)?

Ans.Handles are basically used for controlling the structure of the page like which block will be displayed and where. First level child elements of the node are called layout handles. Every page request can have several unique Handles. The handle is called for every page.

The muster_index_index handle is created by combining the frontName (muster), Action Controller (index), and Action Controller Action Method (index) into a single string and this handle will be called only when /muster/index/index url is accessed.

<layout version="0.1.0">
 <muster_index_index>
 <reference name="root">
 <block type="page/html" name="root" output="toHtml" 
  template="../somepage.phtml" />
 </reference>
 </muster_index_index>
 </layout>

Q 10. How to get the Total Price of items currently in the Cart?

Ans.helper(‘checkout’)->formatPrice(Mage::getSingleton(‘checkout/cart’)
->getQuote()->getGrandTotal()); ?>

Q 11. How to set different themes for logged in users?

if(Mage::getSingleton('customer/session')->isLoggedIn()):
Mage::getDesign()->setPackageName('package_name')->setTheme('themename');
endif;

Q 12. How to create magento custom module?

Ans. Steps to create custom magento module:
Namespace : Zag
Module Name : Mymodule
1. Create directory Mymodule in app/code/local/Zag
2. Create Block, controllers, etc, Module directories. 
Create controller, block and module file as required.
3. Create module configuration file 
(app/code/local/Zag/Mymodule/etc/config.xml).
4. Create xml file (app/etc/modules/Zag_ Mymodule.xml)
to enable/disable module and tell magento system 
from which code pool that module will be taken.

Q 13. How to set different themes for each store?

Ans. Go to : System>Designs
Then, add new design change or edit existing. 
You can select Store and Custom Design.

Q 14. How to fetch 5 bestsellers products programmatically

Ans.
Mage::getResourceModel(‘reports/product_collection’)
->addOrderedQty()
->addAttributeToSelect(‘*’)
->setPage(1, 5)
->load();

Q 15. How to Call a static block in .phtml file.

Ans.

A. Call a static block in .phtml file.
 echo $this->getLayout()->createBlock('cms/block')->setBlockId('YOURBLOCKID')->toHtml();

B. call block directly from phtml file without defining in layout
 echo $this->getLayout()->createBlock(‘newmodule/newblock’)->setTemplate(‘newmodule/newblock.phtml’)->toHtml();   

Advantages and Disadvantages Of using Magento

 Advantages

  •  Flexibility: Magento offers editions based  solution for your business according to your business sizes, needs and budgets .

                     1) Magento Go: Recommended  for Small Merchants and Entrepreneurs.
                    2) Magento Enterprise:  Recommended for the  Medium to Large Businesses.

  • Large Community: Because of its ease, the Magento has earned a large community of customers and developers.
  • Open Source and Available for Free: Magento is an open source platform so available free of cost for the people interested in developing their website.
  • Themes and Templates: To make your online retail store look more attractive, you can use the eye-catching and attractive themes and templates offered by Magento.
  • Offers SEO: SEO stands for Search Engine Optimization which means how you can get your website gain traffic through search engines. If your website is not gaining traffic, Magento offers inbuilt SEO by providing many different types of tools for different type of formatting of the content using which you can optimize your website so that it starts gaining traffic and helps you get a better ranking.
  • Automatic Integration with Google Services:If you are using Magento, many Google services like Google Analytics, Google Base and Google Checkout and these services are used for traffic analysis, adding any type of content and processing orders respectively.
  • Supports Variety of Payment Options: Magento supports payment options like PayPal Express, Standard and Website Payments Pro.
  • Wishlist: Magento wishlist functionality is a  great option to save the items  for the further references and  you can also encourage social shopping  by enabling option so called ‘  Send to a Friend’ and you can share with others.
  • Availability of Customizations:You can customize your website by updating the images, colors, and design according to your wish in the Magento theme applied.
  • Fully flourished with rich features: Magento also comes with simple, configurable, grouped, bundled, virtual and digital product options so that you are able to better organize your stock and sell both products and services at a time on multiple sites.
  • Magento Mobile:  Magento Mobile offers many features like Seamless Storefront Integration, Manage Multiple Devices, Hassle Free Submissions, Fully Customizable, Engage Your  Customers, Low Cost of Ownership.
  • Architecture: Magento provides an architecture which is a bit tough to understand but it fulfills all the needs of the developers.
  • Extensions: Magento provides a wide range of extensions.
  • OOPS concepts: Magento is totally  based on the OOPS concepts.
  • Work becomes easier for developers: Magento makes it easier for the developers to manage and update the web content. It helps the developer in many ways like adding new pages, updating the pages, linking the pages with the main navigation or sub-navigation.

 

 Disadvantages

  • Need Good Hosting Environment: If you don’t use a good server, your customers will feel slow load time which is not good. So as Magento is highly layered the website becomes heavy so it’s better to use reliable and good servers.
  • Large space and memory: To make a Magento website work better, you will require a large amount of space and memory.
  •  Complexity: As Magento is built on Zend Framework and uses the MVC Architecture for programming, it isn’t easy working with Magento.
  •  Limited Technical Documentation: Currently the technical documentation is very limited making it difficult to customize and revise the programming in Magento beyond graphical alteration.
  •  Learning Curve:  As Magento is built on Zend Framework, uses MVC Architecture, uses OOPS concepts so your learning curve increases as we have to have a basic knowledge of the above concepts.
  •  High Maintenance: Installations and upgrades are often painful and time consuming.

History of MAGENTO

Magento is an open source e-commerce web application that was launched on March 31, 2008. It was developed by Varien (now Magento Inc) with help from the programmers within the open source community but is owned solely by Magento Inc. Magento was built using the Zend Framework. It uses the entity-attribute-value (EAV) database model to store data.

Varien, the company owning Magento, formerly worked with osCommerce. They originally planned to fork osCommerce but later decided to rewrite it as Magento. Magento officially started development in early 2007.Seven months later, on August 31, 2007, the first public beta version was released. On May 30, 2010, Magento mobile was released; it allows store owners to create native mobile storefront apps.

In February 2011, eBay announced it had made an investment in Magento in 2010, worth a 49% ownership share of the company.

As of June 6, 2011 eBay owns 100% of Magento. eBay announced that Magento would join eBay’s new X.Commerce initiative.

Magento’s CEO and co-founder Roy Rubin wrote on the Magento blog stating that “Magento will continue to operate out of Los Angeles, with Yoav Kutner and me as its leaders.”.

Yoav Kutner left Magento in April 2012.

As of April 8, 2013 Magento selected Rackspace as its platinum level hosting partner.