B-219 Sec-55 Noida, India
+918010221733

Creating Varien Object & Collection from any Array data

Varien_Object and Varien_Collection are the parent/super class for most of the Magento Models and Collections respectively.

The path for Varien_Object is lib/Varien/Object & for Varien_Collection is lib/Varien/Data/Collection.php.

This article shows how you can create new Varien objects and collections from any array or object data you have.

Here is the code:

/**
 * Creating new varien collection
 * for given array or object
 *
 * @param array|object $items   Any array or object
 * @return Varien_Data_Collection $collection
 */
public function getVarienDataCollection($items) {
    $collection = new Varien_Data_Collection();
    foreach ($items as $item) {
        $varienObject = new Varien_Object();
        $varienObject->setData($item);
        $collection->addItem($varienObject);
    }
    return $collection;
}

(Visited 99 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.