Logging Database Queries in Magento

In lib/Varien/Db/Adapter/Pdo/Mysql.php

around line 103 (for EE 1.13) you find some flags you can set to log DB queries to a file:

protected $_debug = false;

* Minimum query duration time to be logged
* @var float
protected $_logQueryTime = 0.05;

* Log all queries (ignored minimum query duration time)
* @var bool
protected $_logAllQueries = false;

* Add to log call stack data (backtrace)
* @var bool
protected $_logCallStack = false;

* Path to SQL debug data log
* @var string
protected $_debugFile = ‘var/debug/pdo_mysql.log’;

Set $_debug, $_logAllQueries to true.
Give $_debufFile a valid logfile name.
Be sure to set $_debug to false when done trapping queries – this log can fill up FAST!

Magento Templates and Layout Files

Themes allow you to control the look and feel of your Magento installation and the stores it contains. Related themes are grouped together in design packages, and each store in your installation can use its own theme, share a single theme, or any combination of each. The “base package” is the default design package (a collection of related themes) installed.

The files that determine the visual output and frontend functionality of your store are contained inside the themes in a design package. Each design package must contain a default theme and can contain any number of theme variants.


theme-design-treeThemes are found in two main directories in the Magento file system:theme-skin-tree

  • app/design – control how the pages are rendered
  • skin – control the visual aspect of the theme (CSS, images, etc)

Theme Files

  • Layout – the basic XML files that define block structure and control meta information
  • Template – contains the PHTML files containing xHTML markups and the PHP for visual presentation (both page and block templates)
  • Locale – CSV documents with language translation strings for non product and category text within Magento

Skin Files

  • CSS – the CSS files for the theme
  • Images
  • JS – the theme-specific JavaScript routines and callable functions

Theme Fall-back

Any files not customized by a particular theme are pulled from the default theme and base package, allowing developers to localize their themes by changing or adding only those files necessary for a custom theme.

  1. If a custom theme is specified, the application looks in:
    1. app/design/frontend/custom_package/custom_theme
    2. skin/frontend/custom_package/custom_theme
  2. If the file is not found in the custom theme, the application looks in
    1. app/design/frontend/custom_package/default
    2. skin/frontent/custom_package/default
  3. If not found in the default, the application looks in
    1. app/design/frontend/base/default
    2. skin/frontend/base/default
  4. If not found, an error occurs

Magento’s Action Controller does not pass a data object to the view or set properties on the view object but rather directly references system models to get the information it needs for display. The view is built by the combination of Blocks and Templates. Blocks are PHP objects, and Templates are PHP/HTML files. Each Block is tied to a single Template file. Inside a phtml file, PHP’s $this keyword contains a reference to the Template’s Block object.

Magento Codepools


The Magento application contains 3 codepools. Core is reserved for the base classes written by the Core Magento developers. When you want to extend or change the functionality of these classes, you do so by over-riding these classes in your code located in one of the other 2 codepools. Changes to code should never be made in the Core codepool because they may be lost when Magento is upgraded. The Community code pool is where third-party modules, such as the ones downloaded from Magento Connect, can be found. The Local codepool is where any other kind of modification, extension or core (or community) overrides should take place. The Local code pool is loaded first and is the place for changes that need to be made to a specific site or to modify/extend/override what is in the Core or Community codepool.

codepoolconfigDuring the bootstrap, Magento first checks the local codepool for include files, then checks community, and then core. Magento reads a module’s configuration file, looking in the block, to notify the system where to find module code. The module’s configuration file is located in /app/etc/modules.

/app/Mage.php defines the pathing for the three code pools.

Magento Stuck in Maintenance Mode

Sometimes when you install an extension and go to your Magento site you get a 503 error with a message saying your system is in maintenance mode. Often times the culprit is a file called maintenance.flag which should have deleted during installation and didn’t. Browse to your root Magento folder and delete that file. Voila! Magento is up and running again.

Commonly Used Core Magento Block Types

The most commonly used core block types used in Magento layouts

  • core/template: This block renders a template defined by its template attribute. The majority of blocks defined in the layout are of type or subtype of core/template.
  • page/html: This is a subtype of core/template and defines the root block. All other blocks are child blocks of this block.
  • page/html_head: Defines the HTML head section of the page which contains elements for including JavaScript, CSS etc.
  • page/html_header: Defines the header part of the page which contains the site logo, top links, etc.
  • page/template_links: This block is used to create a list of links. Links visible in the footer and header area use this block type.
  • core/text_list: Some blocks like content, left, right etc. are of type core/text_list. When these blocks are rendered, all their child blocks are rendered automatically without the need to call thegetChildHtml() method.
  • page/html_wrapper: This block is used to create a wrapper block which renders its child blocks inside an HTML tag set by the action setHtmlTagName. The default tag is <div> if no element is set.
  • page/html_breadcrumbs: This block defines breadcrumbs on the page.
  • page/html_footer: Defines footer area of page which contains footer links, copyright message etc.
  • core/messages: This block renders error/success/notice messages.
  • page/switch: This block can be used for the language or store switcher.

Insert blocks into the config file with this syntax:

<block type=”page/html” name=”root” output=”toHtml” template=”example/view.phtml”>

where type indicates where in your layout the block will be drawn


The Magento Module

module-structureMagento is a shopping cart application written in PHP with an MVC style architecture. Model-View-Controller applications aim to separate the representation of information contained in the database from the presentation of that information to the user. This architecture pattern leads to increased reuse of code within an application, simplification of design, and ease in maintenance (separation of concerns). The model contains business rules, application data, logic, and functions. The view generates the display of the application data to the user. The controller passes information between the view and the model. Each model in the application separates an area of functionality from others.

To extend the functionality in your Magento, you can create custom modules to do nearly anything you desire. Your custom changes should *never* be placed in the core code so you don’t lose them when you upgrade. Magento contains 3 code pools (groups of modules): core, community, and local. Your modules should be placed in community (if developing for use on multiple magento instances) or local (if developing for your single magento installation).

Namespace refers to a folder used to group similar modules together. What you place in your custom module can override what is in the core or can extend the core with altogether new functionality.

module-structureA module can consist of the following components:

  • Block – contains the layout information
  • Models – implement business logic
  • Resource Models – provide an access layer to data for the extension
  • Controllers – contain methods called actions which process web server requests that correspond to the module
  • Configuration – module-specific XML config files which tell Magento how this module interacts with it
  • Helpers – auxillary classes that implement commonly used business logic – forms, validators, formatters, formatters, etc
  • Schema SQL Scripts – the SQL scripts that implement schema changes for the module
  • Data SQL scripts – scripts which manipulate the data for the SQL schema of the module. NOTE: difference between Enterprise and Community Editions.