在Magento中将属性添加到收集请求 [英] Adding attributes to collection requests in Magento

查看:61
本文介绍了在Magento中将属性添加到收集请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我解决这个问题.我正在尝试使用集合从Magento目录模型中加载一些数据.代码如下:

I hope someone can help me puzzle this one out. I'm trying to load some data out of a Magento catalog model using a collection. The code looks like this:

$model = Mage::getModel('catalog/product');
$collection = $model->getCollection();
$collection->addAttributeToSelect('short_description');
$collection->addFieldToFilter('SKU',array('like' => array('%EBOOK%')));
$collection->load();
var_dump($collection->getData());

这将产生带有平面目录产品表中所有字段的对象转储,而不是我使用$collection->addAttributeToSelect()方法请求的字段.无论我使用此方法指定哪个字段(甚至是'*'),我都无法使集合返回除其标准字段集以外的任何内容.我也无法使用应该起作用的$collection->removeFieldFromSelect(NULL)取消设置任何字段.

This produces a dump of objects with all the fields in the flat catalog product table, but not the field that I have requested with the $collection->addAttributeToSelect() method. No matter what field I specify with this method (even '*'), I cannot get the collection to return anything other than its standard set of fields. I also can't unset any fields using $collection->removeFieldFromSelect(NULL) which is supposed to work.

我在做愚蠢/错误/都是吗?

Am I doing something stupid/wrong/both?

提前谢谢.

推荐答案

这是因为您在集合上调用了getData(),而不是在该集合的产品上调用了getData().

This is because you call getData() on the collection, but not on a product of this collection.

我从来没有真正分析过为什么会发生这种情况,但是如果您使用

I never really analyzed why this happens, but if you use

foreach ($collection as $product) {
    var_dump($product->getData());
}

代替

$collection->load();
var_dump($collection->getData());

您将获得期望的数据.

这篇关于在Magento中将属性添加到收集请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆