简体   繁体   English

Magento:从Varien_Data_Collection获取数据

[英]Magento : Get data from Varien_Data_Collection

Hiho everybody! 大家好! I hope you'll give me a clue about this because I'm still noob with Magento. 我希望你能给我一个线索,因为我还是Magento的老板。

I try to display a list of products I get in an array. 我尝试显示我在数组中获得的产品列表。 In Mage/Catalog/Block/Product/List.php , I created a new Varien_Data_Collection() in which I pushed my products objects (with ->addItem($product) ). Mage/Catalog/Block/Product/List.php ,我创建了一个新的Varien_Data_Collection() ,其中我推送了我的产品对象(使用->addItem($product) )。

Then I return my custom collection and List.php class does his work with it to display the list of products. 然后我返回我的自定义集合, List.php类使用它来显示产品列表。

When I call the page in my browser, I had the right number of displayed products and when I click on it to see the product page, I get the right page. 当我在浏览器中调用页面时,我有正确数量的显示产品,当我点击它以查看产品页面时,我得到了正确的页面。

However, all the data (like the product name, the price, etc) are empty. 但是,所有数据(如产品名称,价格等)都是空的。 I guess that the methods used by List class to catch these data fail with my Varien_Data_Collection object. 我想List类用来捕获这些数据的方法失败了我的Varien_Data_Collection对象。

To illustrate, here is my code sample : 为了说明,这是我的代码示例:

// getting a particular product
$mainProduct = Mage::getModel('catalog/category')->load($currentCat->getId());
$mainProduct = $mainProduct->getProductCollection();
$mainProduct = $mainProduct->addAttributeToFilter('product_id', $_GET['cat_id']);

// creating a custom collection             
$myCollection = new Varien_Data_Collection();

foreach ($mainProduct as $product) {
    // getting my particular product's related products in an array
    $related = $product->getRelatedProductIds();                
    if ($this->array_contains($related, $_GET['cat_id'])) {
        // if it suits me, add it in my custom collection
        $myCollection->addItem($product);
    }
}
return $myCollection;

And this is what I get in my list page : 这就是我在列表页面中得到的内容:

在此输入图像描述

When I var_dump($myCollection) , I can see that ['name'] , ['price'] , etc fields are not referenced. 当我var_dump($myCollection) ,我可以看到没有引用['name']['price']等字段。 Only ['product_id'] and many other fields I don't care about. 只有['product_id']和我不关心的许多其他领域。

My very ultimate question is : how can I return a collection containing these products data to my List class ? 我的最终问题是:如何将包含这些产品数据的集合返回给我的List类? I know that it is poorly explained but my English is very limited and I try to do my best :( 我知道它的解释很差,但我的英语非常有限,我尽力做到最好:(

Calling ->getProductCollection() against a category only returns skeleton data for each product in the created collection. 对类别调用->getProductCollection()仅返回已创建集合中每个产品的框架数据。 If you want full data for each of the products, you need to then load them, so in your foreach loop you would have: 如果你想要每个产品的完整数据,你需要加载它们,所以在你的foreach循环中你将拥有:

$product = Mage::getModel('catalog/product')->load($product->getId());

However the way in which you are building the collection is not the best working practice - you should never have to create your own Varien_Data_Collection object, instead you should be creating a product collection as follows: 但是,构建集合的方式并不是最好的工作方法 - 您永远不必创建自己的Varien_Data_Collection对象,而应该创建一个产品集合,如下所示:

$collection = Mage::getModel('catalog/product')->getCollection();

Then before you load the collecion (which the foreach loop or calling ->load() against the collection will do as 2 examples), you can filter the collection according to your requirements. 然后在加载collecion( foreach循环或对集合调用->load()将作为2个示例)之前,您可以根据您的要求过滤集合。 You can either do this using native Magento methods, one of which you are already using ( addAttributeToFilter() ) or I prefer to pull the select object from the collection and apply filtering this way: 您可以使用本机Magento方法执行此操作,其中一个方法已经在使用( addAttributeToFilter() ),或者我更喜欢从集合中提取select对象并以这种方式应用过滤:

$select = $collection->getSelect();

You can then run all of the Zend_Db_Select class methods against this select object to filter the collection. 然后,您可以针对此select对象运行所有Zend_Db_Select类方法以过滤集合。

http://framework.zend.com/manual/1.12/en/zend.db.select.html http://framework.zend.com/manual/1.12/en/zend.db.select.html

When the collection has been loaded, the products inside it will then contain full product data. 加载集合后,其中的产品将包含完整的产品数据。

first of all pelase do not use $_GET variable, use Mage::app()->getRequest()->getParams(); 首先pelase不使用$ _GET变量,使用Mage::app()->getRequest()->getParams(); second why not try to build your collection correctly from the start? 为什么不尝试从一开始就正确地构建您的集合?

here is what your code does: 这是你的代码所做的:

$mainProduct = Mage::getModel('catalog/category')->load($currentCat->getId());
$mainProduct = $mainProduct->getProductCollection();
$mainProduct = $mainProduct->addAttributeToFilter('product_id', $_GET['cat_id']);

get one product, I mean you load a category then load a product collection, then filter by product id.. why not: 得到一个产品,我的意思是你加载一个类别然后加载产品集合,然后按产品ID过滤..为什么不:

$mainProduct = Mage::getModel('catalog/product')->load($yourSearchedId);

I aslo do not see why you filter products by $_GET['cat_id'] which looks like a category id... 我也不明白为什么你用$ _GET ['cat_id']过滤产品,看起来像是一个类别ID ...

To conclude you can get more help if you explain exactly what you are trying to find. 总而言之,如果您准确解释您想要找到的内容,您可以获得更多帮助。 It looks like you are trying to find all products that have a given product as related. 看起来您正在尝试查找具有相关产品的所有产品。 So why not set for that given product the related product correctly and get the related products collection. 那么为什么不正确地为该给定产品设置相关产品并获得相关产品集合。

$_product->getRelatedProductCollection();

UPDATE: 更新:

now that you cleared your request try this: 既然你已经清除了你的请求,试试这个:

$relatedIds = $product->getRelatedProductIds();
$myCollection = Mage::getModel('catalog/category')
    ->load($currentCat->getId())
    ->getProductCollection();
$myCollection->addAttributeToFilter('product_id', array("in",$relatedIds));

//also addAttributeToSelect all attributes you may need like name etc $myCollection->load(); //也addAttributeToSelect你可能需要的所有属性,如名称等$ $ myCollection-> load(); //maybe you don't actualy need to load here //也许你不需要在这里加载

Please bear in mind I did not test this code it was written from teh top of my head, test it. 请记住,我没有测试这个代码,它是从我的头顶写的,测试它。 But I hope you got the idea. 但我希望你有这个主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM