【问题标题】:How to load products media gallery along with the collection?如何将产品媒体库与收藏一起加载?
【发布时间】:2011-08-19 12:31:10
【问题描述】:

谁能告诉我如何将产品的媒体库与收藏一起加载?

我得到这样的集合:

$collection = Mage::getModel('catalog/product')->getCollection()
                        ->addStoreFilter($storeId)
                        ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
foreach ($collection as $product) {
    var_dump($product->getMediaGalleryImages());
}

但 getMediaGalleryImages() 返回 null。我知道我可以使用 $product = Mage::getModel('catalog/product')->load($product->getId()) 单独加载每个产品,但我想避免这种情况,因为它会导致不必要的工作量。

【问题讨论】:

    标签: magento


    【解决方案1】:

    如果有人正在寻找另一种方法,我发现这是可行的(仅在一种情况下,所以不能保证!):

    请务必先执行$collection->addAttributeToSelect(’image’);,然后在循环收集产品时执行:

    $attributes = $product->getTypeInstance(true)->getSetAttributes($product);
    $media_gallery = $attributes[’media_gallery’];
    $backend = $media_gallery->getBackend();
    $backend->afterLoad($product); //this loads the media gallery to the product object
    

    不确定所有这些是否有必要,但我很着急。在我的特殊情况下,我试图使用 $product->getImageUrl(); 获取图像 url,这种方法对我有用。

    希望它可以帮助别人。

    【讨论】:

    • 关于性能的重要说明: afterLoad() 实际上非常快。获取 $backend 不是。 $backend 变量实际上应该对所有产品都相同,以便可以缓存。
    • 根据 Alex 的评论,我们发现 getSetAttributes($product) 大约需要 50 毫秒才能加载。对我们来说这是一个问题,因为我们加载了许多产品,所以 50 毫秒很重要。如果您缓存(仅对第一个产品运行它们)前 3 行,而不仅仅是 getBackend 行,那么性能(至少对我们而言)会显着提高。
    【解决方案2】:

    我最近也必须这样做,最快的方法:

    class My_Module_Block_Name extends Mage_Catalog_Block_Product_View_Abstract
    {
    
    /** @var null|Mage_Catalog_Model_Resource_Eav_Attribute */
    protected static $_mediaGalleryBackend = null;
    
    public function getGalleryImages()
    {
        $product = $this->getProduct();
        $this->_getBackend()->afterLoad($product);
        $collection = $product->getMediaGalleryImages();
    
        return $collection;
    }
    
    
    /**
     * @return Mage_Catalog_Model_Resource_Eav_Attribute
     */
    protected function _getBackend() {
        if (self::$_mediaGalleryBackend === null) {
    
            $mediaGallery = Mage::getSingleton('eav/config')
                ->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'media_gallery');
    
            self::$_mediaGalleryBackend = $mediaGallery->getBackend();
        }
    
        return self::$_mediaGalleryBackend;
    }
    
    }
    

    【讨论】:

      【解决方案3】:

      试试这个

      $collection = Mage::getModel('catalog/product')->getCollection()
                              ->addStoreFilter($storeId)
                              ->addAttributeToSelect(array('image', 'media_gallery'))
                              ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
      foreach ($collection as $product) {
          var_dump($product->getMediaGallery());
      }
      

      【讨论】:

        【解决方案4】:

        可以直接在循环中使用:

        foreach ($collection as $product) { $product->getResource()->getAttribute('media_gallery')->getBackend()->afterLoad($product); foreach ($product->getMediaGalleryImages() as $image) { var_dump($image->debug()); } }

        【讨论】:

          【解决方案5】:

          你将不得不使用:

          // Returns the Media Gallery Images
          Mage::getModel(’catalog/product’)->load(productid)->getMediaGalleryImages();
          

          参考http://www.magentocommerce.com/boards/viewthread/29639/

          【讨论】:

          • 它加载整个产品。是的,它有效,但需要大量时间和内存。
          • 您推荐的解决方案是什么?
          【解决方案6】:

          处理涉及产品的自定义集合时的秘诀是init 方法的第三个参数……至少对我来说是这样。 这样我就不需要加载整个产品并运行昂贵的查询

          所以,拥有我的自定义 $product 代表 Mage_Catalog_Model_Product 的一个实例,但从我的自定义集合中,我可以这样做:

          (string)Mage::helper('catalog/image')->init($product, 'small_image', $product->getImage())
          

          我还需要将image 属性添加到我的自定义集合中,我通过添加->addAttributeToSelect(['image']) 来做到这一点。

          您还可以相应地调整图像大小:

          (string)Mage::helper('catalog/image')->init($product, 'small_image', $product->getImage())->resize($width, $height=null)
          

          【讨论】:

            【解决方案7】:

            这是一个将媒体库添加到集合的函数:

            // Source: http://www.magentocommerce.com/boards/viewthread/17414/#t141830
            
            public function addMediaGalleryAttributeToCollection(Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $_productCollection) {
                $_mediaGalleryAttributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'media_gallery')->getAttributeId();
                $_read = Mage::getSingleton('core/resource')->getConnection('catalog_read');
            
                $_mediaGalleryData = $_read->fetchAll('
                    SELECT
                        main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
                        `value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`,
                        `default_value`.`position` AS `position_default`,
                        `default_value`.`disabled` AS `disabled_default`
                    FROM `catalog_product_entity_media_gallery` AS `main`
                        LEFT JOIN `catalog_product_entity_media_gallery_value` AS `value`
                            ON main.value_id=value.value_id AND value.store_id=' . Mage::app()->getStore()->getId() . '
                        LEFT JOIN `catalog_product_entity_media_gallery_value` AS `default_value`
                            ON main.value_id=default_value.value_id AND default_value.store_id=0
                    WHERE (
                        main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ') 
                        AND (main.entity_id IN (' . $_read->quote($_productCollection->getAllIds()) . '))
                    ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC    
                ');
            
                $_mediaGalleryByProductId = array();
                foreach ($_mediaGalleryData as $_galleryImage) {
                    $k = $_galleryImage['entity_id'];
                    unset($_galleryImage['entity_id']);
                    if (!isset($_mediaGalleryByProductId[$k])) {
                        $_mediaGalleryByProductId[$k] = array();
                    }
                    $_mediaGalleryByProductId[$k][] = $_galleryImage;
                }
                unset($_mediaGalleryData);
                foreach ($_productCollection as &$_product) {
                    $_productId = $_product->getData('entity_id');
                    if (isset($_mediaGalleryByProductId[$_productId])) {
                        $_product->setData('media_gallery', array('images' => $_mediaGalleryByProductId[$_productId]));
                    }
                }
                unset($_mediaGalleryByProductId);
            
                return $_productCollection;
            }
            

            它的用法示例如下:

            $products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
            $this->addMediaGalleryToArray($products);
            

            【讨论】:

            猜你喜欢
            • 2013-02-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-09
            • 1970-01-01
            • 1970-01-01
            • 2011-12-14
            • 2020-10-31
            相关资源
            最近更新 更多