Converting an array to a collection in Magento

前端 未结 4 870
天命终不由人
天命终不由人 2021-01-14 19:08

I have an array object that is an output of a magento fetchall from DB and i want this to be converted to an object of a Collections class so that i can implement pagination

相关标签:
4条回答
  • 2021-01-14 20:01

    If you have an array of ids $product_ids, you can use:

    $collection = Mage::getModel('catalog/product')->getCollection()
            ->addIdFilter($product_ids);
    
    0 讨论(0)
  • 2021-01-14 20:05

    Also, populating a collection can be done with the addItem method.

    0 讨论(0)
  • 2021-01-14 20:07

    Magento hasn't a built in conversor for that, but you can write you query using Collections, or load the Collection based on all ids from your array.

    0 讨论(0)
  • 2021-01-14 20:08

    In order to convert an array to a collection object:
    1> Create an instance of Varien_Db_Collection

    $collection = new Varien_Data_Collection();
    

    2> Create an instance of Varien_Object and set the array data

    $rowObj = new Varien_Object();
    $rowObj->setData($row);
    

    3> Finally add the Varien_Object to Collection instance

    $collection->addItem($rowObj);
    

    4> Now $collection is a collection object.

    0 讨论(0)
提交回复
热议问题