Direct SQL Queries In Magento


Magento's use of data models provide a great way to access and modify data.

Database Connections In Magento


Magento will automatically connect to it's database and provide two separate resources which you can use to access data: core_read and core_write. As you can probably guess, core_read is for reading from the database while core_write is for writing to the database.
<?php  /**  * Get the resource model  */ $resource = Mage::getSingleton('core/resource');

 /**  * Retrieve the read connection  */ $readConnection = $resource->getConnection('core_read');
 /**  * Retrieve the write connection  */ $writeConnection = $resource->getConnection('core_write');



Get Table names and table prefixes


Get a table name from a string
<?php
 /**  * Get the resource model  */ $resource = Mage::getSingleton('core/resource');

 /**  * Get the table name  */ $tableName = $resource->getTableName('catalog_product_entity');
 /**  * if prefix was 'mage_' then the below statement  * would print out mage_catalog_product_entity  */ echo $tableName;
Get a table name from an entity name
<?php
 /**  * Get the resource model  */ $resource = Mage::getSingleton('core/resource');

 /**  * Get the table name  */ $tableName = $resource->getTableName('catalog/product');
 /**  * if prefix was 'mage_' then the below statement  * would print out mage_catalog_product_entity  */ echo $tableName;


Reading From The Database



Magento models hide the complexity of the EAV system, they sometimes request far more data than is needed. If for example you have a product ID and want it's SKU, it would be much quicker to run a single query to obtain this value than to load in a whole product model (the inverse of this operation is available via the product resource class).
<?php

 /**  * Get the resource model  */ $resource = Mage::getSingleton('core/resource');
 /**  * Retrieve the read connection  */ $readConnection = $resource->getConnection('core_read');
 $query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
 /**  * Execute the query and store the results in $results  */ $results = $readConnection->fetchAll($query);
 /**  * Print out the results  */  var_dump($results);



Writing To The Database



When saving a Magento model, there can be a lot of background data  being saved that you weren't even aware of. For example, saving a product model can take several seconds due to the amount of related data saves and indexing that needs to take place. This is okay if you need all the data saving, but if you only want to update the SKU of a product, this can be wasteful.
The example code below will show you how when given a product ID, you can alter the SKU. This is a trivial example but should illustrate how to execute write queries against your Magento database.


<?php
 /**  * Get the resource model  */ $resource = Mage::getSingleton('core/resource');

 /**  * Retrieve the write connection  */ $writeConnection = $resource->getConnection('core_write');
 /**  * Retrieve our table name  */ $table = $resource->getTableName('catalog/product');

 /**  * Set the product ID  */ $productId = 44;
 /**  * Set the new SKU  * It is assumed that you are hard coding the new SKU in  * If the input is not dynamic, consider using the  * Varien_Db_Select object to insert data  */ $newSku = 'new-sku';
 $query = "UPDATE {$table} SET sku = '{$sku}' WHERE entity_id = "    . (int)$productId;
 /**  * Execute the query  */ $writeConnection->query($query);

Rakesh Singh Uniyal

I’m Rakesh Singh Uniyal (MCA) and I write to help people work on programming and technology. The tips, tutorials and information provided in this blog has helped many people to solve their programming and web development related issues.
I work as a Freelance PHP/Magento/Wordpress Developer.

No comments:

Post a Comment