2

I'm trying to get product collection with all of its images url. Is there any other way to get product collection with all of it image urls?

$collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
        ->joinField(
            'qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left')
        ->joinField(
            'imgurls',
            'catalog/product_attribute_media_gallery',
            'value',
            'entity_id = entity_id',
            null,
            'left')
        ->groupByAttribute('entity_id')
        ->addAttributeToSort('updated_at','asc')
        ->addAttributeToFilter('updated_at', array('from' => $updateAtFilter))
        ->setPageSize($transferLimit)
;
2
  • 1
    Welcome to Magento SE! :) Might I suggest that you add detail to your question? Folks here will be able to help you out much better if you clearly describe the problem, what you have tried and what you're looking to achieve.
    – davidalger
    Dec 10, 2013 at 2:56
  • I'm trying to get product collection with all of its images url. Is there any other way to get product collection with all of it image urls ?
    – user4253
    Dec 10, 2013 at 8:51

3 Answers 3

2

The way Magento stores image will make it difficult if not impossible to get them in one query together with all the other data. Per image type a new row is created in the table catalog_product_entity_media_gallery which means that if you want 5 images for example the query would return 5 rows.

The thumbnail and small image of a product are stored in the the flat tables that means you can access those when calling a product collection. In case you want all images from a product the easiest way is to use the getMediaGalleryImages() method

2
  • Is there any way to get by using group_concat ?
    – user4253
    Dec 10, 2013 at 18:50
  • like this 'productids' => new Zend_Db_Expr('group_concat(sales/order_item.product_id SEPARATOR "|")') in salesorder collection object returns all product ids.
    – user4253
    Dec 10, 2013 at 18:54
2

If you want the URLs directly from the database query, no chance. The URLs are not stored in the database, just a relative file path, from which the URL is derived in PHP.

However, if you are satisfied with a comma separated list of these file paths, it should be possible if you change your code like this:

    ->joinField(
        'imgurls',
        'catalog/product_attribute_media_gallery',
        new Zend_Db_Expr("GROUP_CONCAT({{table}}.value SEPARATOR ','"),
        'entity_id = entity_id',
        null,
        'left')

An alternative would be using Mage_Core_Model_Resource_Helper_Mysql4::addGroupConcatColumn():

Mage::getResourceHelper('core')->addGroupConcatColumn(
    $collection->getSelect(), 'imgurls', 'media_gallery.value');

(after joining the media gallery attribute table with the alias media_gallery)

1

try these it worked for me

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

foreach($collection as $product){
    (string)Mage::helper('catalog/image')->init($product, 'image');
    //Or with resize
    (string)Mage::helper('catalog/image')->init($product, 'image')->resize(200);
}
1
  • I guess there is no 'image' attribute to select. Aug 27, 2015 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.