久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Magento中直接使用SQL語句

 sumi2005 2014-03-27

原理:

 

magento是基于Zend Framework的,所以底層用的還是zend的zend db

 

在文件app/code/core/Mage/Catalog/model/Resource/Eav /Mysql4/Config.php 中追蹤到下面的函數(shù) getAttributesUsedInListing()

Php代碼 復(fù)制代碼 收藏代碼
  1. /**  
  2. * Retrieve Product Attributes Used in Catalog Product listing  
  3. *  
  4. * @return array  
  5. */  
  6. public function getAttributesUsedInListing() {   
  7. $select = $this->_getReadAdapter()->select()   
  8. ->from(array(’main_table’ => $this->getTable(’eav/attribute’)))   
  9. ->join(   
  10. array(’additional_table’ => $this->getTable(’catalog/eav_attribute’)),   
  11. ‘main_table.attribute_id = additional_table.attribute_id’,   
  12. array()   
  13. )   
  14. ->joinLeft(   
  15. array(’al’ => $this->getTable(’eav/attribute_label’)),   
  16. ‘a(chǎn)l.attribute_id = main_table.attribute_id AND al.store_id = ‘ . (int) $this->getStoreId(),   
  17. array(’store_label’ => new Zend_Db_Expr(’IFNULL(al.value, main_table.frontend_label)’))   
  18. )   
  19. ->where(’main_table.entity_type_id=?’, $this->getEntityTypeId())   
  20. ->where(’additional_table.used_in_product_listing=?’, 1);   
  21. – $sql = $select->assemble();   
  22. – echo $sql;   
  23. return $this->_getReadAdapter()->fetchAll($select);   
  24. }  
 

Magento操作數(shù)據(jù)庫是在 Zend DB(Zend Framework)的基礎(chǔ)上簡單的做了下封裝了。Zend DB 有自己的一套規(guī)則,,來組合生成最終的SQL查詢語句,,可以看到上面的代碼中有 from() join() joinLeft() where() 等函數(shù),亂七八糟的一大堆東西,,需要對 Zend DB的規(guī)則非常熟悉,,才能知道實際執(zhí)行的SQL語句,有沒有辦法直接打印出SQL語句?找了下,,還真有,,就是assemble()函數(shù)。在上面代碼中最后 部分可以看到,。順被把SQL也附上來

Sql代碼 復(fù)制代碼 收藏代碼
  1. SELECT `main_table`.*,   
  2. IFNULL(al.value, main_table.frontend_label) AS `store_label`   
  3. FROM `eav_attribute` AS `main_table`   
  4. INNER JOIN `catalog_eav_attribute` AS `additional_table`   
  5. ON main_table.attribute_id = additional_table.attribute_id   
  6. LEFT JOIN `eav_attribute_label` AS `al`   
  7. ON al.attribute_id = main_table.attribute_id AND al.store_id = 1   
  8. WHERE (main_table.entity_type_id=’4′)   
  9. AND (additional_table.used_in_product_listing=1)  

 

Magento中打印SQL語句來調(diào)試

 

有時為了調(diào)試magento商城系統(tǒng),需要獲取當(dāng)前的查詢sql語句,magento中獲取SQL語句,這里我們通過


$collection->getSelectSql(true)來調(diào)試sql

Php代碼 復(fù)制代碼 收藏代碼
  1. $collection=Mage::getResourceModel('reports/product_collection');   
  2. $query=$collection->getSelectSql(true);   
  3. echo $query;  
 

magento獲取SQL語句的另外一種方法是設(shè)置打印SQL為true

Php代碼 復(fù)制代碼 收藏代碼
  1. $collection=Mage::getResourceModel('reports/product_collection');   
  2. $collection->printlogquery(true);  
 

得到的SQL語句

Sql代碼 復(fù)制代碼 收藏代碼
  1. SELECT `e`.* FROM `catalog_product_entity` AS `e`  
 

這里只是打印查詢產(chǎn)品的SQL,如果要獲取其他地方的SQL語句,道理也是一樣的,我們根據(jù)上面的sql語句可以看到,其實magento的性能很差,"select *",magetno又是基于EAV架構(gòu)的,可以想象下這速度

 

操作:

 

Magento的Models 和Collection 很強大,,使用它們可以很方便的查詢和操作數(shù)據(jù)庫。但是有些場合,,因為一些特殊需求或?qū)agento的了解不夠深,,可能會需要自己手寫SQL語句來查詢和操作數(shù)據(jù)庫。以下分別是讀寫數(shù)據(jù)庫的代碼,。

Php代碼 復(fù)制代碼 收藏代碼
  1. // For Read   
  2. // fetch read database connection that is used in Mage_Core module   
  3.   
  4. $read= Mage::getSingleton('core/resource')->getConnection('core_read');   
  5.   
  6. // first way   
  7. $query = $read->query("select name from core_website");   
  8. while ($row = $query->fetch())    
  9. {    
  10.     $row = new Varien_Object($row);     
  11.     echo "<strong>" . $row->getName() . "</strong><br/>";   
  12. }   
  13.   
  14. // second way    
  15. $results = $read->fetchAll("SELECT * FROM core_website;");    
  16. foreach ($results as $row)    
  17. {   
  18.     echo $row['name'] . "<br/>";     
  19. }   
  
Php代碼 復(fù)制代碼 收藏代碼
  1. // For Write   
  2. // fetch write database connection that is used in Mage_Core module   
  3. $write = Mage::getSingleton('core/resource')->getConnection('core_write');   
  4.   
  5. // now $write is an instance of Zend_Db_Adapter_Abstract   
  6. $write->query("insert into tablename values ('aaa','bbb','ccc')");  

 

注意上面的getConnection()方法中的參數(shù) "core_read",,表明了Magento將要使用的資源。與之相對應(yīng),,當(dāng)我們修改數(shù)據(jù)庫的時候使用參數(shù)"core_write".一般情況下 getConnection方法的參數(shù)應(yīng)設(shè)成"core_read" 或 "core_write"(應(yīng)該不指定也是可以的,,但是如果Magento有多個數(shù)據(jù)庫就必須指定了)。

 

作為新的entension module,在config.xml對"core_read" "core_write" 進行定義是個好的習(xí)慣,。定義如下: 

Xml代碼 復(fù)制代碼 收藏代碼
  1. <config>  
  2.     <global>  
  3.         <resources>  
  4.             <extension_setup>  
  5.                 <connection>  
  6.                     <use>core_setup</use>  
  7.                 </connection>  
  8.             </extension_setup>  
  9.             <extension_read>  
  10.                 <connection>  
  11.                     <use>core_read</use>  
  12.                 </connection>  
  13.             </extension_read>  
  14.             <extension_write>  
  15.                 <connection>  
  16.                     <use>core_write</use>  
  17.                 </connection>  
  18.             </extension_write>  
  19.         </resources>  
  20.     </global>  
  21. </config>  
 

對應(yīng)上面新增的module的名字.使用下面相對應(yīng)的語句在read或write Database:

 

Php代碼 復(fù)制代碼 收藏代碼
  1. $conn = Mage::getSingleton('core/resource')->getConnection('extension_read');   
  2. $conn = Mage::getSingleton('core/resource')->getConnection('extension_write');  
 

一般情況是絕大多數(shù)的module都定義成"core_read" "core_write"方便且節(jié)省資源,。當(dāng)然特殊情況除外:

  • 給每個module不同的讀寫權(quán)限
  • 需要用多個Database

實例:

 

Php代碼 復(fù)制代碼 收藏代碼
  1. <?php   
  2.     /**  
  3.      * Get the resource model  
  4.      */  
  5.     $resource = Mage::getSingleton('core/resource');   
  6.     
  7.     /**  
  8.      * Retrieve the read connection  
  9.      */  
  10.     $readConnection = $resource->getConnection('core_read');   
  11.     
  12.     /**  
  13.      * Retrieve the write connection  
  14.      */  
  15.     $writeConnection = $resource->getConnection('core_write');  
  

Get a table name from a string

Php代碼 復(fù)制代碼 收藏代碼
  1. <?php   
  2.     
  3.     /**  
  4.      * Get the resource model  
  5.      */  
  6.     $resource = Mage::getSingleton('core/resource');   
  7.     
  8.     /**  
  9.      * Get the table name  
  10.      */  
  11.     $tableName = $resource->getTableName('catalog_product_entity');   
  12.     
  13.     /**  
  14.      * if prefix was 'mage_' then the below statement  
  15.      * would print out mage_catalog_product_entity  
  16.      */  
  17.     echo $tableName;  
  

Get a table name from an entity name

Php代碼 復(fù)制代碼 收藏代碼
  1. <?php   
  2.     
  3.     /**  
  4.      * Get the resource model  
  5.      */  
  6.     $resource = Mage::getSingleton('core/resource');   
  7.     
  8.     /**  
  9.      * Get the table name  
  10.      */  
  11.     $tableName = $resource->getTableName('catalog/product');   
  12.     
  13.     /**  
  14.      * if prefix was 'mage_' then the below statement  
  15.      * would print out mage_catalog_product_entity  
  16.      */  
  17.     echo $tableName;  
  

Reading From The Database

 

Varien_Db_Select::fetchAll

This method takes a query as it's parameter, executes it and then returns all of the results as an array. In the code example below, we use Varien_Db_Select::fetchAll to return all of the records in the catalog_product_entity table.

Php代碼 復(fù)制代碼 收藏代碼
  1. <?php   
  2.     
  3.     /**  
  4.      * Get the resource model  
  5.      */  
  6.     $resource = Mage::getSingleton('core/resource');   
  7.     
  8.     /**  
  9.      * Retrieve the read connection  
  10.      */  
  11.     $readConnection = $resource->getConnection('core_read');   
  12.     
  13.     $query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');   
  14.     
  15.     /**  
  16.      * Execute the query and store the results in $results  
  17.      */  
  18.     $results = $readConnection->fetchAll($query);   
  19.     
  20.     /**  
  21.      * Print out the results  
  22.      */  
  23.     echo sprintf('<pre>%s</pre>' print_r($results, true));  
  

Varien_Db_Select::fetchCol

This method is similar to fetchAll except that instead of returning all of the results, it returns the first column from each result row. In the code example below, we use Varien_Db_Select::fetchCol to retrieve all of the SKU's in our database in an array.

Php代碼 復(fù)制代碼 收藏代碼
  1. <?php   
  2.     /**  
  3.       * Get the resource model  
  4.       */  
  5.     $resource = Mage::getSingleton('core/resource');   
  6.     
  7.     /**  
  8.      * Retrieve the read connection  
  9.      */  
  10.     $readConnection = $resource->getConnection('core_read');   
  11.     
  12.     /**  
  13.      * Retrieve our table name  
  14.      */  
  15.     $table = $resource->getTableName('catalog/product');   
  16.     
  17.     /**  
  18.      * Execute the query and store the results in $results  
  19.      */  
  20.     $sku = $readConnection->fetchCol('SELECT sku FROM ' . $table . ');   
  21.     
  22.     /**  
  23.      * Print out the results  
  24.      */  
  25.     echo sprintf('<pre>%s</pre>' print_r($results, true));  
 

Try this code and look at the results. Notice how all of the SKU's are in a single array, rather than each row having it's own array? If you don't understand this, try changing fetchCol for fetchAll and compare the differences.

 

Varien_Db_Select::fetchOne

Unlike the previous two methods, Varien_Db_Select::fetchOne returns one value from the first row only. This value is returned on it's own and is not wrapped in an array. In the code example below, we take a product ID of 44 and return it's SKU.

Php代碼 復(fù)制代碼 收藏代碼
  1. <?php   
  2.     
  3.     /**  
  4.      * Get the resource model  
  5.      */  
  6.     $resource = Mage::getSingleton('core/resource');   
  7.     
  8.     /**  
  9.      * Retrieve the read connection  
  10.      */  
  11.     $readConnection = $resource->getConnection('core_read');   
  12.     
  13.     /**  
  14.      * Retrieve our table name  
  15.      */  
  16.     $table = $resource->getTableName('catalog/product');   
  17.     
  18.     /**  
  19.      * Set the product ID  
  20.      */  
  21.     $productId = 44;   
  22.     
  23.     $query = 'SELECT sku FROM ' . $table . ' WHERE entity_id = '  
  24.              . (int)$productId . ' LIMIT 1';   
  25.     
  26.     /**  
  27.      * Execute the query and store the result in $sku  
  28.      */  
  29.     $sku = $readConnection->fetchOne($query);   
  30.     
  31.     /**  
  32.      * Print the SKU to the screen  
  33.      */  
  34.     echo 'SKU: ' . $sku . '<br/>';  
 

When trying out this example, ensure you change the product ID to an ID that exists in your database!

 

You may think that fetchOne works the same as fetchCol or fetchAll would if you only added 1 column to the SELECT query and added a 'LIMIT 1', however you would be wrong. The main difference with this function is that the value returned is the actual value, where as Varien_Db_Select::fetchCol and Varien_Db_Select::fetchAll would wrap the value in an array. To understand this a little, try swapping the method's and comparing the 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代碼 復(fù)制代碼 收藏代碼
  1. <?php   
  2.     
  3.     /**  
  4.      * Get the resource model  
  5.      */  
  6.     $resource = Mage::getSingleton('core/resource');   
  7.     
  8.     /**  
  9.      * Retrieve the write connection  
  10.      */  
  11.     $writeConnection = $resource->getConnection('core_write');   
  12.     
  13.     /**  
  14.      * Retrieve our table name  
  15.      */  
  16.     $table = $resource->getTableName('catalog/product');   
  17.     
  18.     /**  
  19.      * Set the product ID  
  20.      */  
  21.     $productId = 44;   
  22.     
  23.     /**  
  24.      * Set the new SKU  
  25.      * It is assumed that you are hard coding the new SKU in  
  26.      * If the input is not dynamic, consider using the  
  27.      * Varien_Db_Select object to insert data  
  28.      */  
  29.     $newSku = 'new-sku';   
  30.     
  31.     $query = "UPDATE {$table} SET sku = '{$sku}' WHERE entity_id = "  
  32.              . (int)$productId;   
  33.     
  34.     /**  
  35.      * Execute the query  
  36.      */  
  37.     $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.

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多