Using Custom Table for Custom Module in Magento
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
[source language=”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>
[/source]
Then Create a Model
[source language=”php”]
<?php
class YNamespace_Ymodule_Model_Mymodel extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init(‘ymodel/mymodel’);
}
}
[/source]
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.
[source language=”php”]
<?php
class Ynamespace_Ymodel_Model_Mysql4_Ymodel extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init(‘ymodel/mymodel’, ‘id’);
}
}
[/source]
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.
[source language=”php”]
$model=Mage::getModel("ymodel/mymodel")
->setName("Subesh")
->setLastname("Pokhrel")
->save();
[/source]
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
[source language=”php”]
$model=Mage::getModel("ymodel/mymodel")->load($id)
->setName("Subesh")
->setLastname("Pokharel")
->save();
[/source]
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
[source language=”php”]
<?php
class Ynamespace_Ymodel_Model_Mysql4_Mymodel_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init(‘ymodel/mymodel’);
}
}
[/source]
To Select All the row of Table
[source language=”php”]
$allCollection=Mage::getModel("ymodel/mymodel")->getCollection();
[/source]
You can add filters in the above query as required.
To get individual Row you can just loop through the collection :)
[source language=”php”]
foreach($allCollection as $objRow){
print_r($objRow);
}
[/source]
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… :P.