简体   繁体   English

如何将自定义数据添加到已加载的Magento集合

[英]How to add custom data to loaded Magento collection

I've been working on merging two different collections together and I'm finding it very hard to do. 我一直在努力将两个不同的集合合并在一起,但我发现很难做到。

Actually, the only thing I'm trying to do is adding another property to an already instantiated (I think) collection. 实际上,我唯一要做的就是将另一个属性添加到已实例化(我认为)的集合中。

This is my code: 这是我的代码:

private function _addProductsToBanners()
{
    $skus = $this->_getBannersSkus();

    $products = Mage::getResourceModel('catalog/product_collection')
        ->addFieldToFilter('sku', array('in' => $skus))
        ->addAttributeToSelect(array('name'));

    /** @var Developer_Banners_Model_Resource_Banner_Collection $banners */
    $banners = $this->banners;

    foreach ($banners as $banner) {
        foreach ($products as $product) {
            if ($banner->getSku() == $product->getSku())
            {
                $banner->setData('product', 1);
            }
        }
    }
    echo '<pre>';
    print_r($banners->getData());
    echo '</pre>';
}

The problem here is, that when printing the $banners collection with getData() I can't see the [product] property, but if I do it without getData() then I see it. 这里的问题是,当使用getData()打印$banners集合时,我看不到[product]属性,但是如果不使用getData()则会看到它。

If you have any ideas I would greatly appreciate it! 如果您有任何想法,我将不胜感激!

Thanks! 谢谢!

When Developer_Banners_Model_Resource_Banner_Collection inherits from Mage_Core_Model_Resource_Db_Collection_Abstract and you add an item, the item is not added to the $_data property but to the $_items property. Developer_Banners_Model_Resource_Banner_Collection继承自Mage_Core_Model_Resource_Db_Collection_Abstract并添加项目时,该项目不会添加到$ _data属性,而是添加到$ _items属性。

protected function _addItem($item)
{
   $this->_items[] = $item;
   return $this;
}

When you call then the getData() method, the $_data property is still null. 当您调用getData()方法时,$ _ data属性仍为null。 The getData() method will then fetch the data from the DB and return the result. 然后,getData()方法将从数据库中获取数据并返回结果。

public function getData()
{
    if ($this->_data === null) {


        $this->_renderFilters()
             ->_renderOrders()
             ->_renderLimit();
        /**
         * Prepare select for execute
         * @var string $query
         */
        $query       = $this->_prepareSelect($this->getSelect());
        $this->_data = $this->_fetchAll($query, $this->_bindParams);
        $this->_afterLoadData();
    }
    return $this->_data;
}

By just iterating on the object the each() method is executed: 通过仅对对象进行迭代,即可执行each()方法:

public function each($obj_method, $args=array())
    {
        foreach ($args->_items as $k => $item) {
            $args->_items[$k] = call_user_func($obj_method, $item);
        }
    }

This method uses the $_items property as data source. 此方法使用$_items属性作为数据源。 Unfortunately I can't explain this behaviour right now. 不幸的是,我现在无法解释这种行为。

我发现的是,直到您实际调用getData() ,才设置集合_data属性,但是实际上设置了每个集合成员上的_data ,因此可以在迭代集合时访问新添加的属性。

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

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