1

I have doubt with product collection

<?php 

    $categoryIds = array(469);//category id
    $products = Mage::getResourceModel('catalog/product_collection')
    ->addCategoryFilter(Mage::getModel('catalog/category')->load($categoryIds));
    $Collection = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array('in' => $products->getAllIds()));;
    $Collection->addAttributeToSelect(array(
                                       'image','small_image',
                                       'name',
                                       'short_description'
                       ))
    ->addFieldToFilter('visibility', array(
                                   Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                                   Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                       )) 
    ->setOrder('price', 'ASC')

    ->joinField('qty',
                 'cataloginventory/stock_item',
                 'qty',
                 'product_id=entity_id',
                 '{{table}}.stock_id=1',
                 'left')
     ->addAttributeToFilter('qty', array("gt" => 0))

    ->addFinalPrice()
    ->getSelect()
    ->where('price_index.final_price < price_index.price')
    ?>

I wrote in this collection to my phtml it's working fine. But what is the drawback to written collection in phtml

I feel after that site little bit slow. Then I wrote MVC But i don't know how to call in that phtml.

  • where you add this function?? mean model or block? – Qaisar Satti Mar 28 '16 at 5:20
  • @QaisarSatti i don't where add and how to call can you please explain me – Magento 2 Mar 28 '16 at 5:21
  • Also i want to know what is the draw back if i written in phtml – Magento 2 Mar 28 '16 at 5:23
2

The phtml file is a view, so you should not work directly on database there. Instead try to create a model, use that in the block and then call the prepared data from the block object.

  • Ok thanks What's drawback if i will write in phtml – Magento 2 Mar 28 '16 at 5:28
  • There is none. it will work but you must follow the mvc structure. – Qaisar Satti Mar 28 '16 at 5:30
  • Okay But i feel site is slow after written this under phtml – Magento 2 Mar 28 '16 at 5:32
  • query take same time to execute. check the query how much time it take echo $products->getSelect(); – Qaisar Satti Mar 28 '16 at 5:35
  • How to check how much time it will take it dispaly SELECT e.*, cat_index.position AS cat_index_position FROM catalog_product_entity AS e INNER JOIN catalog_category_product_index AS cat_index ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.category_id = '469' – Magento 2 Mar 28 '16 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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