Magento中直接使用SQL语句
原理:
magento是基于Zend Framework的,所以底层用的还是zend的zend db
在文件app/code/core/Mage/Catalog/model/Resource/Eav /Mysql4/Config.php 中追踪到下面的函数 getAttributesUsedInListing()
/**
* Retrieve Product Attributes Used in Catalog Product listing
*
* @return array
*/
public function getAttributesUsedInListing() {
$select = $this->_getReadAdapter()->select()
->from(array(’main_table’ => $this->getTable(’eav/attribute’)))
->join(
array(’additional_table’ => $this->getTable(’catalog/eav_attribute’)),
‘main_table.attribute_id = additional_table.attribute_id’,
array()
)
->joinLeft(
array(’al’ => $this->getTable(’eav/attribute_label’)),
‘al.attribute_id = main_table.attribute_id AND al.store_id = ‘ . (int) $this->getStoreId(),
array(’store_label’ => new Zend_Db_Expr(’IFNULL(al.value, main_table.frontend_label)’))
)
->where(’main_table.entity_type_id=?’, $this->getEntityTypeId())
->where(’additional_table.used_in_product_listing=?’, 1);
– $sql = $select->assemble();
– echo $sql;
return $this->_getReadAdapter()->fetchAll($select);
}
Magento操作数据库是在 Zend DB(Zend Framework)的基础上简单的做了下封装了。Zend DB 有自己的一套规则,来组合生成最终的SQL查询语句,可以看到上面的代码中有 from() join() joinLeft() where() 等函数,乱七八糟的一大堆东西,需要对 Zend DB的规则非常熟悉,才能知道实际执行的SQL语句,有没有办法直接打印出SQL语句?找了下,还真有,就是assemble()函数。在上面代码中最后 部分可以看到。顺被把SQL也附上来
SELECT `main_table`.*,
IFNULL(al.value, main_table.frontend_label) AS `store_label`
FROM `eav_attribute` AS `main_table`
INNER JOIN `catalog_eav_attribute` AS `additional_table`
ON main_table.attribute_id = additional_table.attribute_id
LEFT JOIN `eav_attribute_label` AS `al`
ON al.attribute_id = main_table.attribute_id AND al.store_id = 1
WHERE (main_table.entity_type_id=’4′)
AND (additional_table.used_in_product_listing=1)
Magento中打印SQL语句来调试
有时为了调试magento商城系统,需要获取当前的查询sql语句,magento中获取SQL语句,这里我们通过
$collection->getSelectSql(true)来调试sql
$collection=Mage::getResourceModel('reports/product_collection');
$query=$collection->getSelectSql(true);
echo $query;
magento获取SQL语句的另外一种方法是设置打印SQL为true
$collection=Mage::getResourceModel('reports/product_collection');
$collection->printlogquery(true);
得到的SQL语句 :
SELECT `e`.* FROM `catalog_product_entity` AS `e`
这里只是打印查询产品的SQL,如果要获取其他地方的SQL语句,道理也是一样的,我们根据上面的sql语句可以看到,其实magento的性能很差,"select *",magetno又是基于EAV架构的,可以想象下这速度
操作:
Magento的Models 和Collection 很强大,使用它们可以很方便的查询和操作数据库。但是有些场合,因为一些特殊需求或对Magento的了解不够深,可能会需要自己手写SQL语句来查询和操作数据库。以下分别是读写数据库的代码。
// For Read
// fetch read database connection that is used in Mage_Core module
$read= Mage::getSingleton('core/resource')->getConnection('core_read');
// first way
$query = $read->query("select name from core_website");
while ($row = $query->fetch())
{
$row = new Varien_Object($row);
echo "<strong>" . $row->getName() . "</strong><br/>";
}
// second way
$results = $read->fetchAll("SELECT * FROM core_website;");
foreach ($results as $row)
{
echo $row['name'] . "<br/>";
}
// For Write
// fetch write database connection that is used in Mage_Core module
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $write is an instance of Zend_Db_Adapter_Abstract
$write->query("insert into tablename values ('aaa','bbb','ccc')");
注意上面的getConnection()方法中的参数 "core_read",表明了Magento将要使用的资源。与之相对应,当我们修改数据库的时候使用参数"core_write".一般情况下 getConnection方法的参数应设成"core_read" 或 "core_write"(应该不指定也是可以的,但是如果Magento有多个数据库就必须指定了)。
作为新的entension module,在config.xml对"core_read" "core_write" 进行定义是个好的习惯。定义如下:
<config>
<global>
<resources>
<extension_setup>
<connection>
<use>core_setup</use>
</connection>
</extension_setup>
<extension_read>
<connection>
<use>core_read</use>
</connection>
</extension_read>
<extension_write>
<connection>
<use>core_write</use>
</connection>
</extension_write>
</resources>
</global>
</config>
对应上面新增的module的名字.使用下面相对应的语句在read或write Database:
$conn = Mage::getSingleton('core/resource')->getConnection('extension_read');
$conn = Mage::getSingleton('core/resource')->getConnection('extension_write');
一般情况是绝大多数的module都定义成"core_read" "core_write"方便且节省资源。当然特殊情况除外:
- 给每个module不同的读写权限
- 需要用多个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 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
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
/**
* 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
*/
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
/**
* Get the resource model
*/
$resource = Mage::getSingleton('core/resource');
/**
* Retrieve the read connection
*/
$readConnection = $resource->getConnection('core_read');
/**
* Retrieve our table name
*/
$table = $resource->getTableName('catalog/product');
/**
* Execute the query and store the results in $results
*/
$sku = $readConnection->fetchCol('SELECT sku FROM ' . $table . ');
/**
* Print out the results
*/
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
/**
* Get the resource model
*/
$resource = Mage::getSingleton('core/resource');
/**
* Retrieve the read connection
*/
$readConnection = $resource->getConnection('core_read');
/**
* Retrieve our table name
*/
$table = $resource->getTableName('catalog/product');
/**
* Set the product ID
*/
$productId = 44;
$query = 'SELECT sku FROM ' . $table . ' WHERE entity_id = '
. (int)$productId . ' LIMIT 1';
/**
* Execute the query and store the result in $sku
*/
$sku = $readConnection->fetchOne($query);
/**
* Print the SKU to the screen
*/
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
/**
* 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.
Magento中直接使用SQL语句的更多相关文章
- Magento中如何调用SQL语句
I. 创建表结构和测试数据 create table rooms(id int not null auto_increment, name varchar(100), primary key(id)) ...
- SQL Server中常用的SQL语句(转):
SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...
- 如何找出MySQL数据库中的低效SQL语句
面对业务的迅猛发展,DBA的一项重要工作就是及时发现数据库中的低效SQL语句,有的可以立刻着手解决(比如缺少合适的索引),有的需要尽快反馈给开发人员进行修改. MySQL数据库有几个配置选项可以帮助我 ...
- SQL*PLUS中批量执行SQL语句
SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...
- 在ADO.NET中使用参数化SQL语句访问不同数据库时的差异
在ADO.NET中经常需要跟各种数据库打交道,在不实用存储过程的情况下,使用参数化SQL语句一定程度上可以防止SQL注入,同时对一些较难赋值的字段(如在SQL Server中Image字段,在Orac ...
- SQL点滴32—Excel中CONCATENATE函数生成SQL语句
原文:SQL点滴32-Excel中CONCATENATE函数生成SQL语句 当拿到一个Excel的时候需要将这里面的数据插入到数据库里面,该怎么办,除了使用SSIS数据导入之外还可以使用Excel中的 ...
- 在程序开发中怎样写SQL语句可以提高数据库的性能
以下内容是公司dba总结. 1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来 ...
- Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项) A.cat B.concat C.join D.+ 解答:B
- 面试、笔试中常用的SQL语句(数据库知识必杀)一共50个!!!
Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 ...
随机推荐
- delphi -- 进制转换 函数表
1.16 TO 10 ******************************************************** 16转10,否则输出-1 function Hex(c: cha ...
- nyoj 236 心急的C小加
心急的C小加 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的 ...
- hdoj 2122 Ice_cream’s world III
并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 浅谈OC运行时(RunTime)
常说OC是一门动态语言,这使得OC开发可以变得相当灵活,但是归根结底OC还是一种编译型的语言,其具有一定的动态性,但是其动态特性也比不上JavaScript这种解释型的语言.OC的动态性主要体现在这个 ...
- VB.Net中点击按钮(Button)会重复提交两次表单
在VB.NET程序开发过程中遇到一个问题 提交一个表单时,button的html代码如下 <asp:Button ID="btnSubmit" OnClick="c ...
- 你的iOS静态库该减肥了
我有点震惊,国内的具有极少的有关静态库开发的内容,除了一些简单的如何教你开发,而且是原创还是copy,谁知道呢?细思极恐,不扯那么远了,这里就已切身的爬坑经历提供几个静态库瘦身的方案. 公司最近开发完 ...
- MSSQLSERVER数据库- 数据类型Timestamp
Timestamp:亦称时间戳数据类型,它提供数据库范围内的惟一值,反应数据库中数据修改的相对顺序,相当于一个单调上升的计数器.当它所定义的列在更新或者插入数据行时,此列的值会被自动更新,一个计数值将 ...
- js-弹出一个新窗口 新窗口自动转接到一个页面然后自动关闭
这个问题.好. 在百度问问找到的:他的做法是打开一个后通过实例对象,再进行一将跳转,最后再将JS里定时将实例对象关闭... 这个问题其实不需要两个页面,只要三行JS代码就能实现,除非你在2.php里面 ...
- 二、FreeMarker 模版开发指南 第二章 数值和类型
章节内容如下: 基本内容 类型 一.基本内容 简介 什么是数值? 什么是类型? 数据模型是哈希表 a.简介 理解数值和类型的概念是理解数据模型的关键所在.然而,数值和类型的概念并不局限于数据模型, ...
- hadoop 2.0 详细配置教程
http://www.cnblogs.com/scotoma/archive/2012/09/18/2689902.html