Magento中直接使用SQL语句
原理:
?
magento是基于Zend Framework的,所以底层用的还是zend的zend db
?
在文件app/code/core/Mage/Catalog/model/Resource/Eav /Mysql4/Config.php 中追踪到下面的函数 getAttributesUsedInListing()
??<?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);?To test this has worked, use the knowledge gained from the first part of this tutorial to write a query to extract the SKU that has just been changed.
?
Varien_Db_Select
The Varien_Db_Select, which has been touched on in this article is a far better option for extracting/wriiting information. Not only is it easy to use, it also provides a layered of security, which if used correctly, is impenetrable. More will be covered on Varien_Db_Select (aka Zend_Db_Select) in a future article.
?
?
来源:http://fishpig.co.uk/blog/direct-sql-queries-magento.html
?
?
?
?
?