How to convert an array to a collection object in magento?

November 6, 2012  |  2 Comments  |  by Raj (MagePsycho)  |  Latest, Magento


Suppose say we have an array of data fetched from database.
And we want to merge it with Magento collection object or want to provide the data as collection object to the view(template) file.


$resource		= Mage::getModel('core/resource');
$connection		= $resource->getConnection('core_read');
$sql			= "SELECT * FROM custom_table WHERE some_field = ?";
$rows			= $connection->fetchAll($sql, array($someFieldValue));//this row will return an array

$collection = new Varien_Db_Collection();
foreach($rows as $row){
	$rowObj = new Varien_Object();

//now you can get the data using collection way
foreach($collection as $_data){

In order to create a collection object
1> Create an instance of Varien_Db_Collection

$collection = new Varien_Db_Collection();

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

$rowObj = new Varien_Object();

3> Finally add the Varien_Object to Collection instance


Now you can play with the magic getters and setters of collection class.

In order to have depth knowledge on Magento Collection you can refer to the Alan Storm’s article:
Varien Data Collections

Playing with Attribute Set in Magento

November 18, 2011  |  5 Comments  |  by Raj (MagePsycho)  |  Latest, Magento


An attribute set is a collection of attributes which can be created/edited from Catalog -> Manage Attribute Sets in backend.

Some useful snippets regarding Attribute Set.

1> Getting Attribute Set Details

$attributeSetModel = Mage::getModel('eav/entity_attribute_set');
$attributeSetId	   = 9; // or $_product->getAttributeSetId(); if you are using product model

2> Getting Attribute Set Id by Name

$entityTypeId = Mage::getModel('eav/entity')
$attributeSetName	= 'Default';
$attributeSetId		= Mage::getModel('eav/entity_attribute_set')
					->addFieldToFilter('attribute_set_name', $attributeSetName)
echo $attributeSetId;

Note that the following code doesn’t work

$attributeSetId = Mage::getModel('eav/entity_attribute_set')
->load($attributeSetName, 'attribute_set_name')

Because if you refer to eav_attribute_set table then you can see there are lots of rows with same attribute set name(for example: Default)

3> Getting all Attribute Sets by Entity Type(catalog_product)

$entityTypeId = Mage::getModel('eav/entity')
$attributeSetCollection = Mage::getModel('eav/entity_attribute_set')
foreach($attributeSetCollection as $_attributeSet){

Alternatively, you can also use:

$attributeSets = Mage::getModel('catalog/product_attribute_set_api')->items();
foreach($attributeSets as $_attributeSet){

4> Getting all attributes in an Attribute Set

$attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
foreach($attributes as $_attribute){

5> Filtering Products by Attribute Set

$productCollection = Mage::getModel('catalog/product')
					->addFieldToFilter('attribute_set_id', $attributeSetId);
foreach($productCollection as $_product){

Hope you will find these snippets useful/helpful.
Please do share if you have any useful code snippets regarding Attribute Set.

Thanks for reading!