时间:2011-05-05 15:41:28

标签: magento

任何人都可以给我一个关于如何加载产品的媒体库以及集合的提示。 我收到这样的收藏:

$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())单独加载每个产品,但我想避免这种情况,因为它会导致不必要的工作量。

谢谢!

7 个答案:

答案 0 :(得分:18)

如果有人在寻找另一种方法,我发现这是有用的(只有一种情况,所以没有保证!):

请务必先$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();获取图像网址,这种方法对我有用。

希望它可以帮助别人。

答案 1 :(得分:9)

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

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;
}

}

答案 2 :(得分:2)

试试这个

$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());
}

答案 3 :(得分:1)

您将不得不使用:

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

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

答案 4 :(得分:1)

它可以直接在循环中使用:

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

答案 5 :(得分:0)

使用涉及产品的自定义集合时的秘诀是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)

希望这会有所帮助。

答案 6 :(得分:-6)

这是一个将媒体库添加到集合的功能:

// 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);