magento中的joinAttribute joinField

       通常我们使用Mage::getModel('catalog/product')->getCollection()能够获取到产品集。

在这个产品集中,只包含了table  catalog_product_entity 中的字段,但是很多时候我们都需要更多的字段,比如产品price,qty等;

一般情况下,我们会这样做:

[html]  view plain copy
  1. $collection = Mage::getModel('catalog/product')->getCollection();  
  2.   
  3. $collection->addAttributeToSelect('filedname');//通过验证,此处添加这一行不起作用  
  4.   
  5. foreach($collection->getAllIds() as $id){  
  6.   
  7.     $prodMage::getModel('catalog/product')->load($id)->getData();//此时的prod包含了EAV模型中的所有属性  
  8.   
  9. }  

实际上,上面虽然能获取产品的所有属性,但是在你的product有上万条的时候,就会严重影响性能了。或许你会说用下边的code也可以:

 

[html]  view plain copy
  1. $collection->addExpressionFieldToSelect('filedname', "{{filedname}}", array('filedname' => 'filedname'));  
  2. $collection->getSelect()->joinLeft('tablename','e.entity_id=`tablename`.entity_id','');  
不错,虽然可以得到你想要的属性,但是这仅仅限于系统属性,如qty,因为我们知道qty是存储于 table cataloginventory_stock_item 中的,但是

 

magento中又很多自定义属性,是采用EAV模型存储的,所以上面的方法也不可行了。

还好有 joinAttribute  joinField这两个function解决了上面的问题。

比如我想将name添加到collection中

 

[html]  view plain copy
  1. $collection->joinAttribute('name','catalog_product/name', 'entity_id', 'entity_id','left', 0);  
就可以了,将qty添加到collection中

 

 

[html]  view plain copy
  1. $collection->joinField('qty', 'cataloginventory/stock_item','qty','product_id=entity_id', '{{table}}.stock_id=1','left');  
就可以了。

 

joinAttribute  用来添加EAV模型中的Attribute  ,只要在table eav_attribute中能找到的attribute_code都可以在这里使用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值