If you want to create a custom module in Magento that has something to do with storing data into a custom table and using that later, then this is just the right post you have bumped into! I will presume that you already know about Models in Magento, what are they and how you can create them. Also I will presume that you will also know the basic module structure in Magento.
You will have to place this in your config.xml
<global>
<ymodel>
<class>YNamespace_YModule_Model</class>
<resourceModel>quotes_mysql4</resourceModel>
</ymodel>
<quotes_mysql4>
<class>YNamespace_Ymodule_Model_Mymodel</class>
<entities>
<mymodel>
<table>ymodel_ytable</table>
</mymodel>
</entities>
</quotes_mysql4>
</global>
Then Create a Model
<?php
class YNamespace_Ymodule_Model_Mymodel extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('ymodel/mymodel');
}
}
As you can see in constructor of this class it inits the model (tag) defined in the config file. As model uses Resources, you now need to create a resource the module uses.
<?php
class Ynamespace_Ymodel_Model_Mysql4_Ymodel extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('ymodel/mymodel', 'id');
}
}
So using this resource you now can access Table and add/alter. But you must not explicitly create a object of this class to do that. Creating a model of class above will make this resource usable.
$model=Mage::getModel("ymodel/mymodel")
->setName("Subesh")
->setLastname("Pokhrel")
->save();
This will create a new row in table now given that name and lastname is the columns of the table. To edit row of a table do the following
$model=Mage::getModel("ymodel/mymodel")->load($id)
->setName("Subesh")
->setLastname("Pokharel")
->save();
Now to select, there is another procedure. Before that let me tell about collection. Think collections as the array of objects.When you Query in database you get result as collections in Magento and every row resulting from the query is a different Object.To create a collection
<?php
class Ynamespace_Ymodel_Model_Mysql4_Mymodel_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('ymodel/mymodel');
}
}
To Select All the row of Table
$allCollection=Mage::getModel("ymodel/mymodel")->getCollection();
You can add filters in the above query as required.
To get individual Row you can just loop through the collection
foreach($allCollection as $objRow){
print_r($objRow);
}
Hope this helps if any one is in need… and @inchoo..please don’t twitt this as any other..link…you find in a weekend…
.

This really helped me clarify a config issue I had. Thanks for posting it.
Hi Subesh
Sir you are doing very nice work , and sharing your work is helping other people to learn easier. I want to thank you for giving your time and sharing it , Really very good job , thanks a ton
Sir I have to add a module which will store creditcard information in the left tab of customer my account , I created a new module -> with http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table , and with your blog too.
1 Table got created named creditcard
2 I am successful in fetching data through that table on frontend .
3 How to store data in the creditcard from frontend
4 And During fetching it is showing all the rows , how to limit it to customer specific fetching
Thanks for the compliments..
As I see you have two problems left one is storing data from frontend and next is retrieving customer specific data.
For the First, you can go through the admin controller of the Module Created to save data. You just need to set the post array and assign it to the Model and save.
Next on retrieving data you need to add a function collection to retrieve individual data. Please post what can you do or what have to done and I may guide you further.
Thanks.
LoL, sorry Subesh, I didn’t see this till now
Tweeted now.
Is it possible to use custom table rows as attributes of a product?
For example, I have a Country table and I need to let the admin select countries that an electronic product can work in.
I don’t think country can be an attribute because I also need to save country in the user profile and have some info per country.
Thanks