- 浏览: 81736 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
shuishui8310:
开公司了?
Magento学习课程继续 -
yanggaojiao:
对不起,很久没上这里了,在CMS->Home->D ...
Magento在首页自定制Block的方法 -
beautiful_good:
hi,你好我用的是magento 1.4.2.0版本的,请问如 ...
Magento在首页自定制Block的方法 -
as3291363:
太慢了....
Magento 1.4.1.0 的CMS太酷了 -
richardlovejob:
这个分享是在太及时了,谢谢。
Magento Events分析和使用实例与技巧
这节打算介绍一下Magento的Varien_Data_Collections,我从Alanstorm翻译过来的(部分翻译,读者也可以练一下英文能力)
Varien Data Collections
这是什么东西?哈哈,你看完下面的文章,你就会知道它在Magento中的核心的作用了。
作为一个PHPer,如果想把一些一组相关的变量集合在一起有两个方法:Array 和自定义的数据结构,Varien_Object 就是后者。
首先介绍一下 Varien_Object 的使用方法:
$thing_1 = new Varien_Object();
$thing_1->setName('Richard');
$thing_1->setAge(24);
$thing_2 = new Varien_Object();
$thing_2->setName('Jane');
$thing_2->setAge(12);
$thing_3 = new Varien_Object();
$thing_3->setName('Spot');
$thing_3->setLastName('The Dog');
$thing_3->setAge(7);
Varient_Object 类是 Magento 中所有的 Models 的父类。继承自Varient_Object 的子类都有 getter 和 setter 两个魔术方法(更多关于魔术方法请参考:)。例子:
var_dump($thing_1->getName());
如果你不知道Varient_Object 的成员属性有哪些,可以getData()把它们打印出来
var_dump($thing_3->getData());
输出如下
array
'name' => string 'Spot' (length=4)
'last_name' => string 'The Dog' (length=7)
'age' => int 7
$thing_1->setLastName('Smith');
大家注意到了没'last_name’多了'_',你可以以Camel 式名称来getter和setter它。
var_dump($thing_3["last_name"]);
上面我们已经建了三个Object,接着把它们放到Collection中(这里只Varento_Data_Collection),Collection是PHPer自定义的数据类型。
$collection_of_things = new Varien_Data_Collection();
$collection_of_things
->addItem($thing_1)
->addItem($thing_2)
->addItem($thing_3);
由于Magento中大部分的data Collections都是继承自Varient_Data_Collection,所以该上面的使用方法在Magento中随处可见。
foreach($collection_of_things as $thing)
{
var_dump($thing->getData());
}
也可以直接访问第一个或者最后一个成员
var_dump($collection_of_things->getFirstItem());
var_dump($collection_of_things->getLastItem()->getData());
或者可以把它转换成XML格式
var_dump( $collection_of_things->toXml() );
或者只提取其中的一个Column的值(Varient_Data_Collection这如其名,它对应SQL语句的查询结果)
var_dump($collection_of_things->getColumnValues('name'));
甚至提供了过滤的能力(可以说SQL的查询条件和排序功能在Magento的Varient_Data_Collection中都实现了)
var_dump($collection_of_things->getItemsByColumnValue('name','Spot'));
Model Collections
因此,这是个非常好的体验,但是有什么重要的作用呢?因为上面我们说过Magento内建的data Collections都继承自它。这就意味着:你可以对一个product Collection 进行sort:
public function testAction()
{
$collection_of_products = Mage::getModel('catalog/product')->getCollection();
var_dump($collection_of_products->getFirstItem()->getData());
}
大部分的Magento Model objects 都有一个名字为 getCollection的方法,该方法返回上述的 Collection,并且系统中Magento Model Objects 默认初始化并返回该Collection.
同Magento中其他大部分的Collection一样,product Collection 继承自Varient_Data_Collection_Db,这样就提供很多有用的方法给我们获取具体的select statement:
public function testAction()
{
$collection_of_products = Mage::getModel('catalog/product')->getCollection();
var_dump($collection_of_products->getSelect()); //might cause a segmentation fault
}
上面的方法将输出
object(Varien_Db_Select)[94]
protected '_bind' =>
array
empty
protected '_adapter' =>
...
哈哈!由于Magento 实现了Zend framework的数据库抽象层,所以你的SQL语句是以Object的形式出现的如:
public function testAction()
{
$collection_of_products = Mage::getModel('catalog/product')->getCollection();
//var_dump($collection_of_products->getSelect()); //might cause a segmentation fault
var_dump(
(string) $collection_of_products->getSelect()
);
}
结果如下:
'SELECT `e`.* FROM `catalog_product_entity` AS `e`'
更多时候是更复杂的一些查询:
string 'SELECT `e`.*, `price_index`.`price`, `price_index`.`final_price`, IF(`price_index`.`tier_price`, LEAST(`price_index`.`min_price`, `price_index`.`tier_price`),`price_index`.`min_price`) AS `minimal_price`, `price_index`.`min_price`,`price_index`.`max_price`, `price_index`.`tier_price` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0'
此外由于Magento中部分数据表是设计成EAV形式的,由于column那么多不可能都返回,默认返回部分column,你可以以下面的方式返回全部Column的结果:
$collection_of_products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*'); //the asterisk is like a SQL SELECT *
或者在默认的基础上增加一个Column:
//or just one
$collection_of_products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('meta_title');
或者增加多个Column:
//or just one
$collection_of_products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('meta_title')
->addAttributeToSelect('price');
关于Magento 的 Lazy Loading
由于PHP支持了ORM,所以一般情况下,我们在写一个SQL语句或初始化一个对象的时候,该查询语句将立刻执行
$model = new Customer();
//SQL Calls being made to Populate the Object
echo 'Done'; //execution continues
而在Magento中,使用了 Lazy Loading
的方法,使得该语句没有立刻执行。 Lazy Loading,简而言之就是只有当client-programmer 需要读取数据的时候才会执行相应的查询语句。像下面这样子
$collection_of_products = Mage::getModel('catalog/product')
->getCollection();
实际上之时Magento还没有去读取数据库,接下来你可以增加一些attibutes to select:
$collection_of_products = Mage::getModel('catalog/product')
->getCollection();
$collection_of_products->addAttributeToSelect('meta_title');
下面是一些对Database Collection进行过滤的更多方法(看原文吧)
The most important method on a database Collection is addFieldToFilter
. This adds your WHERE
clauses. Consider this bit of code, run against the sample data database (substitute your own SKU is you’re using a different set of product data)
public function testAction()
{
$collection_of_products = Mage::getModel('catalog/product')
->getCollection();
$collection_of_products->addFieldToFilter('sku','n2610');
//another neat thing about collections is you can pass them into the count //function. More PHP5 powered goodness
echo "Our collection now has " . count($collection_of_products) . ' item(s)';
var_dump($collection_of_products->getFirstItem()->getData());
}
The first parameter of addFieldToFilter
is the attribute you wish to filter by. The second is the value you’re looking for. Here’s we’re adding a sku
filter for the value n2610
.
The second parameter can also be used to specify the type of filtering you want to do. This is where things get a little complicated, and worth going into with a little more depth.
So by default, the following
$collection_of_products->addFieldToFilter('sku','n2610');
is (essentially) equivalent to
WHERE sku = "n2610"
Take a look for yourself. Running the following
public function testAction()
{
var_dump(
(string)
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('sku','n2610')
->getSelect());
}
will yield
SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (e.sku = 'n2610')'
Keep in mind, this can get complicated fast if you’re using an EAV attribute. Add an attribute
var_dump(
(string)
Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('meta_title','my title')
->getSelect()
);
and the query gets gnarly.
SELECT `e`.*, IF(_table_meta_title.value_id>0, _table_meta_title.value, _table_meta_title_default.value) AS `meta_title`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_varchar` AS `_table_meta_title_default`
ON (_table_meta_title_default.entity_id = e.entity_id) AND (_table_meta_title_default.attribute_id='103')
AND _table_meta_title_default.store_id=0
LEFT JOIN `catalog_product_entity_varchar` AS `_table_meta_title`
ON (_table_meta_title.entity_id = e.entity_id) AND (_table_meta_title.attribute_id='103')
AND (_table_meta_title.store_id='1')
WHERE (IF(_table_meta_title.value_id>0, _table_meta_title.value, _table_meta_title_default.value) = 'my title')
Not to belabor the point, but try not to think too much about the SQL if you’re on deadline.
Other Comparison Operators
I’m sure you’re wondering “what if I want something other than an equals by query”? Not equal, greater than, less than, etc. The addFieldToFilter
method’s second parameter has you covered there as well. It supports an alternate syntax where, instead of passing in a string, you pass in a single element Array.
The key of this array is the type of comparison you want to make. The value associated with that key is the value you want to filter by. Let’s redo the above filter, but with this explicit syntax
public function testAction()
{
var_dump(
(string)
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('sku',array('eq'=>'n2610'))
->getSelect()
);
}
Calling out our filter
addFieldToFilter('sku',array('eq'=>'n2610'))
As you can see, the second parameter is a PHP Array. Its key is eq
, which stands for equals
. The value for this key is n2610
, which is the value we’re filtering on.
Magento has a number of these english language like filters that will bring a tear of remembrance (and perhaps pain) to any old perl developers in the audience.
Listed below are all the filters, along with an example of their SQL equivalents.
array("eq"=>'n2610')
WHERE (e.sku = 'n2610')
array("neq"=>'n2610')
WHERE (e.sku != 'n2610')
array("like"=>'n2610')
WHERE (e.sku like 'n2610')
array("nlike"=>'n2610')
WHERE (e.sku not like 'n2610')
array("is"=>'n2610')
WHERE (e.sku is 'n2610')
array("in"=>array('n2610'))
WHERE (e.sku in ('n2610'))
array("nin"=>array('n2610'))
WHERE (e.sku not in ('n2610'))
array("notnull"=>'n2610')
WHERE (e.sku is NOT NULL)
array("null"=>'n2610')
WHERE (e.sku is NULL)
array("gt"=>'n2610')
WHERE (e.sku > 'n2610')
array("lt"=>'n2610')
WHERE (e.sku < 'n2610')
array("gteq"=>'n2610')
WHERE (e.sku >= 'n2610')
array("moreq"=>'n2610') //a weird, second way to do greater than equal
WHERE (e.sku >= 'n2610')
array("lteq"=>'n2610')
WHERE (e.sku <= 'n2610')
array("finset"=>array('n2610'))
WHERE (find_in_set('n2610',e.sku))
array('from'=>'10','to'=>'20')
WHERE e.sku >= '10' and e.sku <= '20'
Most of these are self explanatory, but a few deserve a special callout
in, nin, find_in_set
The in
and nin
conditionals allow you to pass in an Array of values. That is, the value portion of your filter array is itself allowed to be an array.
array("in"=>array('n2610','ABC123')
WHERE (e.sku in ('n2610','ABC123'))
notnull, null
The keyword NULL is special in most flavors of SQL. It typically won’t play nice with the standard equality (=
) operator. Specifying notnull
or null
as your filter type will get you the correct syntax for a NULL comparison while ignoring whatever value you pass in
array("notnull"=>'n2610')
WHERE (e.sku is NOT NULL)
from - to filter
This is another special format that breaks the standard rule. Instead of a single element array, you specify a two element array. One element has the key from
, the other element has the key to
. As the keys indicated, this filter allows you to construct a from/to range without having to worry about greater than and less than symbols
public function testAction
{
var_dump(
(string)
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('price',array('from'=>'10','to'=>'20'))
->getSelect()
);
}
The above yields
WHERE (_table_price.value >= '10' and _table_price.value <= '20')'
AND or OR, or is that OR and AND?
Finally, we come to the boolean operators. It’s the rare moment where we’re only filtering by one attribute. Fortunately, Magento’s Collections have us covered. You can chain together multiple calls to addFieldToFilter
to get a number of “AND” queries.
function testAction()
{
echo(
(string)
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('sku',array('like'=>'a%'))
->addFieldToFilter('sku',array('like'=>'b%'))
->getSelect()
);
}
By chaining together multiple calls as above, we’ll produce a where clause that looks something like the the following
WHERE (e.sku like 'a%') AND (e.sku like 'b%')
To those of you that just raised your hand, yes, the above example would always return 0 records. No sku can begin with BOTH an a
and a b
. What we probably want here is an OR
query. This brings us to another confusing aspect of addFieldToFilter
’s second parameter.
If you want to build an OR
query, you need to pass an Array of filter Arrays in as the second parameter. I find it’s best to assign your individual filter Arrays to variables
public function testAction()
{
$filter_a = array('like'=>'a%');
$filter_b = array('like'=>'b%');
}
and then assign an array of all my filter variables
public function testAction()
{
$filter_a = array('like'=>'a%');
$filter_b = array('like'=>'b%');
echo(
(string)
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('sku',array($filter_a,$filter_b))
->getSelect()
);
}
In the interest of being explicit, here’s the aforementioned Array of filter Arrays.
array($filter_a,$filter_b)
This will gives us a WHERE clause that looks something like the following
WHERE (((e.sku like 'a%') or (e.sku like 'b%')))
Wrap Up
You’re now a Magento developer walking around with some serious firepower. Without having to write a single line of SQL you now know how to query Magento for any Model your store or application might need.
发表评论
-
Magento Admin Form表单元素大全
2012-09-30 12:08 1037开发Magento后台的时候经常用到表单(Varien_Dat ... -
Magento A-Z和sitemap插件
2012-09-30 11:53 3741Iifire历经四个月多开发 ... -
Magento SQL绑定查询条件
2012-04-17 15:17 2640Magento从数据库中取数据时大多用的Collection, ... -
Magento站内信插件
2012-02-28 10:04 1482Magento Message Box即站内信组件,主要用于管 ... -
Magento內链外链插件
2012-02-21 10:26 1479给您的Magento网站添加关键词的链接,更多的内链和外链,更 ... -
Magento高级产品订阅
2012-02-21 08:54 2280基本功能介绍 产品订 ... -
Magento产品降价通知和到货通知插件
2012-02-09 16:04 01.客户可以在Magento前台页面订阅某个产品的降价通知(缺 ... -
Magento学习课程继续
2011-03-20 16:05 1492由于前一大段时间都比较忙,忽略了大家啦,好久没有更新内容了。打 ... -
Magento在首页自定制Block的方法
2010-09-19 14:27 2834Magento中想在首页显示一些自定制的BLock有很多中方法 ... -
magento获取相应的查询语句
2010-09-19 14:12 1082虽然Magento对数据库层封装得比较深,但是我们可以很轻松的 ... -
Magento 使用小技巧体现大智慧(Last things first)
2010-06-15 23:38 1288在我们的购物车Magento上线之前,有些小细节很容易被忽略。 ... -
从数据库设计看Magento系列教程(1)
2010-06-15 19:11 0TODO..... -
Magento Events分析和使用实例与技巧
2010-06-14 20:15 3400当你需要扩展Magento的核心的功能时有两个选择 重写( ... -
Magento CMS 应用实例
2010-06-14 19:03 4805下面是一些CMS的应用实例: 本文已移到 http:// ... -
Magento 1.4.1.0 的CMS太酷了
2010-06-14 02:49 1716哇,Magento 1.4.1.0版本已经出来了,今天安装使用 ... -
Magento SEO 使用技巧
2010-06-13 20:50 1117这些内容大致分为: HTML Head URL rewr ... -
Magento web services API 扩展
2010-06-13 11:52 3127<?xml version="1.0&qu ... -
Magento image 操作
2010-06-13 10:48 1318由于lib/Varien/Image.php 的 Varie ... -
Magento中直接使用SQL查询语句
2010-06-13 10:29 1800本文已移到:http://www.iifire.com ... -
在导航栏中或其他位置添加Home链接
2010-06-13 08:47 2532本节不打算翻译,留给自己需要的时候Copy用 A ...
相关推荐
`Varien_Object`和`Varien_Data_Collection`这两个核心类为开发者提供了一套完善的工具集,使得开发者能够轻松地管理和操作数据。理解这些基础知识对于成为一名高效的Magento程序员至关重要。 总之,Magento通过其...
magento-ce-2.4.0_sample_data-2020-07-26-02-51-57.tar.gz Include sample data
mysqldump -u [username] -p[password] magento_db > backup.sql ``` 这里的`[username]`和`[password]`是数据库的用户名和密码,`magento_db`是Magento的数据库名。备份完成后,将`backup.sql`文件传输到新服务器...
magento2-Ho_Templatehints, h& 2高级模板提示模块 h& 2高级模板提示模块[Overview $0 ] ( 文档/总 workings.gif )Ho_Templatehints扩展了默认的Magento模板提示。使用肌肉存储器 ?ath=1 轻松访问。显示hints模
《IWD Magento 2 Checkout Suite Pro 1.6.0:优化支付流程的全面解析》 在电商领域,用户体验是决定商家成功与否的关键因素之一,尤其是支付环节。Magento作为全球领先的开源电商平台,其扩展库中有一款名为“IWD ...
5. **Promotions Manager**: "promotions_manager_for_magento_2_user_guide.pdf"是用户指南,详细介绍了如何使用此模块创建和管理促销活动,包括设置促销条件、限制、有效期,以及跟踪促销效果。 6. **...
1. **advanced_permissions_for_magento_2_user_guide.pdf**:这是该模块的用户手册,详细介绍了如何使用和配置高级权限功能,包括创建和管理用户角色,设置权限规则,以及解决可能出现的问题等。 2. **installing_...
Magento-CE-2.3.4_sample_data-2020-01-22-11-11-58.tar.gz源自官网
第一次上传文件,请多多关照。 自己写的magento2中batch压缩图片,以及把压缩文件的原图备份起来。
Magento 2 是一个开源的电子商务平台,以其高度可定制性和强大的功能著称。WebShopApps MatrixRate 是 Magento 2 平台上的一款扩展,主要用于处理复杂的运费计算规则。这个压缩包 "Magento2的WebShopAppsMatrixRates...
标题 "phoenix-module-cashondelivery-1.0.2_lady4mf_magento_magento2_Pho" 提供的信息表明这是一个 Magento 2 模块,由 PhoenixModule 创建,版本号为 1.0.2,并且可能与 lady4mf 有关。这个模块专注于 "Cash on ...
3. **功能兼容性**:模板兼容Magento 1.4到1.9版本,意味着它支持这些版本中的核心功能,如分类导航、产品展示、购物车、结账流程等,同时可能还包含了特定的增强功能,如快速查看、愿望清单等。 4. **UI/UX设计**...
该模板的核心在于Bootstrap的集成。Bootstrap的网格系统使得页面布局易于调整,无论是在大屏桌面还是移动设备上,都能保持良好的可读性和可操作性。同时,Bootstrap的预定义样式和JavaScript插件,如导航栏、下拉...
### Magento中文用户指南知识点概述 #### 一、Magento简介 ...以上内容覆盖了Magento中文用户指南中的关键知识点,旨在帮助新手快速掌握Magento的核心功能与操作技巧,同时也为有经验的用户提供了进阶指南。
该标题"New Arrivals模块_magento_magento新品模块_bank15p_"暗示了这是一个专门针对Magento系统的新品展示模块,可能由名为"bank15p"的开发者或团队创建。"bank15p"可能是这个模块的独特标识或者是开发者的用户名,...
当我们谈论“dollstore_magento_1.2_weidea.net_BootstrapWordpress前端源码模板”时,我们可以看到一个巧妙的结合,即将WordPress的前端设计优势与Magento的电子商务功能相结合的创新实践。 Bootstrap,由Twitter...
### Magento Certified Developer 认证题库解析 #### 题目一:加载产品属性的条件 **题目描述**:为了使一个属性能够在目录/产品对象上加载,以下哪两个条件必须满足?(选择两项) A. eav_attribute表必须包含一...
Magento 2在巴西葡萄牙语(pt_BR)中的翻译 包括所有Magento 2版本的最新翻译本语言包是根据生成的,并已转换为GIT存储库以方便安装和更新。兼容的Magento 2开源Magento 2商业Magento 2 B2B安装通过作曲家要通过安装...
apw_php_magento_google_data_feed 使用DOM Document XML用PHP编写的Google Merchant Data Feed for Magento,此脚本按原样提供,在使用前需要进行修改,它仅作为示例发布,不用于商业用途。 有关此脚本的详细说明...