Archive for March, 2010


In this post I will show you how to write a basic magento model to do AMD activities to a db table.

Under package “Mypackage” & module “Mymod” we will create model Test so that we can do AMD with table named “test”.

1.Here we first create a table test in db.

CREATE TABLE `test` (
`test_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 25 ) NOT NULL
) ENGINE = MYISAM 

2.Create shell module Mymod under package Mypackage

//file:- /app/etc/modules/Mypackage_Mymod.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mypackage_Mymod>
            <active>true</active>
            <codePool>local</codePool>
        </Mypackage_Mymod>
    </modules>
</config> 

3.Write config.xml for Mymod module. Here we declate a handler for table test .
//file:-/app/code/local/Mypackage/Mymod/etc/config.xml

/
<?xml version="1.0"?>
<config>
    <modules>
        <Mypackage_Mymod>
            <version>0.1.0</version>
        </Mypackage_Mymod>
    </modules>
    <global>
        <models>
			<mymod>
				<!-- Init model for mymod module -->
				<class>Mypackage_Mymod_Model</class>
				<!-- Init db config handler for mymod models -->
				<resourceModel>mymod_mysql4</resourceModel>
			</mymod>
			
			<!-- declaring model vs db table relation -->
            <mymod_mysql4>
                <class>Mypackage_Mymod_Model_Mysql4</class>
				<!-- declate table test -->
                <entities>
                    <test>
                        <table>test</table>
                    </test>
                </entities>
				<!-- -/- -->
            </mymod_mysql4>
			<!-- -/- -->
        </models>
		
		
		<!-- Setup db read & write connection for Mymod module -->
		<resources>
			<!-- db write connection -->
			<mymod_write>
				<connection>
					<use>core_write</use>
				</connection>
			</mymod_write>
			<!-- db read connection -->
			<mymod_read>
				<connection>
					<use>core_read</use>
				</connection>
			</mymod_read>
		</resources>
		<!-- -/- -->
    </global>
</config>

4.Write model Test.php. Here we configure this model with the handler of table test.
file:-/app/code/local/Mypackage/Mymod/model/Test.php

<?php

class Mypackage_Mymod_Model_Test extends Mage_Core_Model_Abstract
{
	
	public function _construct()
    {
        parent::_construct();
        $this->_init('mymod/test');
    }
}

5.Create the resource model for model test.
file:-/app/code/local/Mypackage/Mymod/model/Mysql4/Test.php. here we also configure the primary key id of the table test.

<?php

class Mypackage_Mymod_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {    
        $this->_init('mymod/test', 'test_id');
    }
}

6.Create a collection class so that we can retrive data from table test.
file:-/local/Mypackage/Mymod/model/Mysql4/Test/Collection.php

<?php

class Mypackage_Mymod_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('Mymod/test');
    }
}

That’s it we are done. Now we can do AMD operation with table test with model test.

	$model = Mage::getModel('mymod/test')
		->setName('Asad Rahman')
		->save();

Also use the test collection model to retrive collection data from table test.

	$model = Mage::getModel('mymod/test')
	$data = $model->getCollection()->getData();
	print_r($data);
$productId = 10; 
$attributeName = 'my_attribute_name';

$product = Mage::getModel('catalog/product')->load($productId);
$attributes = $product->getAttributes();

$attributeValue = null;		
if(array_key_exists($attributeName , $attributes)){
	$attributesobj = $attributes["{$attributeName}"];
	$attributeValue = $attributesobj->getFrontend()->getValue($product);
}
echo $attributeValue; //attribute value for 'my_attribute_name'

Here is a code snippet exaple how you fetch all products by it’s attribute set name:-

//Fetch attribute set id by attribute set name
$attrSetName = 'my_custom_attribute';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
	->load($attrSetName, 'attribute_set_name')
	->getAttributeSetId();

//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
	->getCollection()
	->addAttributeToSelect('name')
	->addFieldToFilter('attribute_set_id', $attributeSetId);

//process your product collection as per your bussiness logic
$productsName = array();
foreach($products as $p){
	$productsName[] = $p->getData('name');
}
//return all products name with attribute set 'my_custom_attribute'
print_r($productsName); 

That’s it, have happy time with magento 🙂 !!!