Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Sunday, June 23, 2013

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:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
/**
 * 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;
}

1 comment: