Home » Magento

Magento: Creating Varien Object & Collection from any Array data

10 September 2012 Share/Bookmark

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;
}

More details on Varien Data Objects & Collections can be found on this Magento Knowledge Base Article:
Magento for Developers: Part 8 – Varien Data Collections

From Mukesh Chapagain's Blog, post Magento: Creating Varien Object & Collection from any Array data

email

php magento mukesh chapagain

Get New Post by Email
RSS Feed Subscribe RSS Feed
  • http://twitter.com/SeotecMedia Seotec Media

    Very interesting, but often it’s much more faster to use multidimensional arrays.

  • Francesco Magazzu’

    it is not difficult to give support for pagination as well …