0

I am doing a fetch from product model,

    $collection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('thumbnail')
                    ->addExpressionAttributeToSelect('query_text', '{{name}}', 'name')
                    ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                    ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                    ->addAttributeToFilter('name', array('like' => '%'.$this->getQueryText().'%'))
                    ->addAttributeToSort('name', 'ASC')
                    ->setPageSize(10)
                    ->addStoreFilter($this->getStoreId());

                     $data = serialize($collection->getData());
                     $data = unserialize($data);

Here I am serializing the getdata() from product collection, also before rendering on frontend I am unserializing it and looping on it for fetchinng product related info, but Ia m unable to get the product image thumbnail link when I am serializing and unserializing the collection data, when I just pass the collection object and do a loop I get all product info ( image too) .

foreach ($collection as $item) {
                print_r($item);
                $_data = array(
                    'title' => $item['query_text'],
                    'row_class' => (++$counter)%2?'odd':'even',
                    'img'=>$this->helper('catalog/image')->init($item, 'thumbnail')->resize(100),
                    'id'=>$item['entity_id'],
                );

                if ($item['query_text'] == $query) {
                    array_unshift($data, $_data);
                }
                else {
                    $data[] = $_data;
                }
            }

I am serializing the data for storing it in CACHE, any help on how to retrieve the thumbnail from unserialized serialized data ?

img'=>$this->helper('catalog/image')->init($item, 'thumbnail')->resize(100) //this is not working 

1 Answer 1

1

Instead of store whole collection in to cache i would request you to store only necessary information in to cache. So it will not take much time to load and to get information very fast.

so do like this

in model just write data with below function in to cache and get same collection of array in to view file for display.

 public function getProductForstore($product)
    {
        return  array(
                    'title' => $item['query_text'],
                    'row_class' => (++$counter)%2?'odd':'even',
                    'img'=>$this->helper('catalog/image')->init($item, 'thumbnail')->resize(100),
                    'id'=>$item['entity_id'],
                );
        );
    }

load cache key

   $cache = Mage::app()->getCache();
   $cacheKey = 'YOUR_CUSTOM_KEY';
    if ($cacheProducts = $cache->load($cacheKey)) {
            return json_decode($cacheProducts, true);
        }
     else {   
// fetch product and save in your custom key 
    }

i suggest Do above work in block. or you can also directly add in to your model.

hope this will be very helpful to you.

3
  • looks cool , so you mean getting all the neccssary info and storing it in cache right ? lemme try bro :) Oct 5, 2015 at 7:00
  • Heylo it worked and ty for making it more optimized ! Shukriya Dost Oct 5, 2015 at 7:18
  • Glad i could help you.:)
    – liyakat
    Oct 5, 2015 at 7:38

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.