Saturday, August 11, 2012

Magento Database Repair Tool


The Database Repair Tool compares 2 databases (reference [”corrupted”] and target), and updates the target database so it has the same structure as the reference database by doing the following:
  • Adds missing tables, or repairs them to have the same engine type and charset
  • Adds missing fields or repairs them
  • Removes inconsistent data from tables that have broken foreign key references
  • Adds missing foreign keys and indexes
A typical use case for this tool is to fix the database of an existing Magento installation that has some of the errors mentioned above.

Usage Instructions

Crash-course for the impatient

Install the same version of Magento you’re using into a clean database. Use the new database as “reference” and the current database as “corrupted”.
That’s it :)
Below come step-by-step instructions.

Test it before running on a Production Environment!

Warning! Before running the repair tool on a production environment, make sure you have tested it before on a development environment. Make sure to create a backup of your original database before running this tool
It is also highly encouraged to restrict access to your website while repairing the database. Here is an example of restricting your Magento instance to your IP address exclusively. Other visitors, including search spiders, will get the HTTP503 Service Unavailable error.
Create a file 503.php in your Magento installation root:
  1. <?php
  2. header('HTTP/1.1 503 Service Unavailable');
  3. header('Content-Type: text/plain; charset=UTF-8');
  4. echo "503 Service Unavailable";
In .htaccess or in Apache server configuration, add the following rewrite rule:
Where 127.0.0.1 (note the backslashes before dots) should be replaced with your IP-address.
Once you save this .htaccess file or reload Apache configuration, your site will be down until you restore the initial state.

Step-by step

  1. Download Magento database repair tool archive from the download page
  2. Uncompress the archive
  3. Put the magento-db-repair-tool-1.0.php into any folder on your server
  4. Backup your existing database to have ability to restore it if anything goes wrong
  5. Clone it as new database on the same server. Let’s call it “database2”
  6. Create an empty database (”database3”)
  7. Either copy your entire Magento folder (without cache and sessions) into a new one and install there into “database3” (By editing local.xml with credentials for the newly created “database3”);
  8. Or if you already restricted access to your Magento instance, you may just change your database credentials into “database3”, clean cache and launch Magento once: it will be installed automatically.
At this point you should have the clone of your original database in “database2” and a brand new “database3” with empty Magento installed.
  1. Enter access credentials to “database2” as “corrupted” database and to “database3” as “reference” database
  2. Set table prefixes, if applicable
  3. Press “Continue” and you will see result screen where you will see what was done to the “corrupted” database.
Explanation of the report:
  1. If nothing was changed, then there is no need to fix your database
  2. Only table charset was changed — usually there is no need to worry about it, especially if these tables don’t have text data
  3. Table engine was changed from MyIsam to InnoDb — major issue. Needs developer for investigation
  4. Added missing foreign key or field (or even a table!) — major/fatal issue. Ask a developer for help.
If you are satisfied with database repair report and need to fix your live database, you can either switch your installation to the “database2” (because it was repaired), or perform the repair directly on the live database.
Don’t forget to remove the magento-db-repair-tool-1.0.php and restore access to website when you are done.

Get Ready for Magento Certified Developer Exam. Magento Module Structure.


Typical Magento Module structure has the following folders and is based on MVC architecture.
Let’s review all parts of module structure step by step.
Block
This folder liability is View, if we use terms of classical MVC architecture. Blocks coordinate models with the template files. The files in the folder load the data from database and transfer it to the templates in your theme (.phtml files).
Controllers
Controllers represent all business logic actions for the given request (dispatch(), preDispatch(), postDispatch() methods) and delegate commands to other parts of the system.
etc
It contains all xml files that declare and configure behavior of all modules. Each module must have at least config.xml and it’s a right place to declare all models, routers, blocks, helpers, etc.
Optionally this folder could have adminhtml.xml (grant permissions for your tabs/sections in backend menus) and system.xml (creates this new section or specifies where it should be displayed in existing one).
Find more about xml structure here.
Helper
Here you can create methods that would be useful for your store in different ways. Helpers contain utility methods, which are commonly used in the whole system. Methods, declared in helpers, can be called from any template file or block, model, controller class by
Each Module has a default Data Helper class Modulename/Helper/Data.php and we can call it as
Model
In classical MVC, Models are used to connect to the database and process the data from and to it. Magento has a different approach, which can be tricky at first look. Here is what they say:
“Most Magento Models can categorized in one of two ways. There’s a basic, ActiveRecord-like/one-object-one-table Model, and there’s also an Entity Attribute Value (EAV) Model. Each Model also gets a Model Collection. Collections are PHP objects used to hold a number of individual Magento Model instances. The Magento team has implemented the PHP Standard Library interfaces of IteratorAggregate and Countable to allow each Model type to have it’s own collection type. If you’re not familiar with the PHP Standard Library, think of Model Collections as arrays that also have methods attached. Magento Models don’t contain any code for connecting to the database. Instead, each Model uses two modelResource classes, (one read, one write), that are used to communicate with the database server (via read and write adapter objects).”
sql
Handles any custom database tables which will be used by the module and process all upgrades to the extension.
etc/modules/Namespace_Modulename.xml
To tell Magento which modules should be used and where they are located, we need to create XML file in the folder, from which it takes all xml files – etc/modules/
You can indicate additional parameters such as depends, version and platform (can be defined in module’s config.xml as well):
Depends
Version
Platform

Add to Cart with custom attributes & values


I want to display the custom price on the Shopping Cart page. By default if you add to cart the product it will take the price & will show that price on the cart page.
The data on the cart page is coming from the table ‘sales_flat_quote_item’. This table is having lot many fields. Some of them are product_id, name, description, qty, price. Those are the fields which basically reflects on the cart page.
My task was to display the custom image, custom price, custom description in the row of a product on the cart page.
So first of all we need to insert those values in the DB, while adding the product to the cart.
The phase of adding the product to cart is bit complicated to understand.
These are list of some functions involved in Add to cart phase.
app/code/core/Mage/Checkout/controllers/CartController.php: addAction()
app/code/core/Mage/Checkout/Model/Cart.php: addProduct($product, $info)
app/code/core/Mage/Sales/Model/Quote.php: addProduct(Mage_Catalog_Model_Product $product, $request=null)
In the above function, you can set the custom values for the product.
Find the for loop:
foreach ($cartCandidates as $candidate) { … }
Inside that custom fields can be set.
01foreach ($cartCandidates as $candidate) {
02            $item $this->_addCatalogProduct($candidate$candidate->getCartQty());
03            ...
04            ...
05            ...
06            $item->setCustomPrice('custom value goes here');
07            $item->setCustomImage('custom value goes here');
08            ...
09            ...
10}
I want the custom image as well. So I have added the one more field in the table ‘sales_flat_quote_item’.
Getting those custom values on the cart page is very easy. Go to template/checkout/cart/item/default.phtml.
The below lines can get those values.
1echo $_item->getCustomImage();
The price on the cart page will replaced automatically by custom_price value if that has been set.