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);
Hi
I have implemented all steps mentioned by you in “Magento model to interact with db table”. But I am unable to see any module created in admin with name “Mymod” and pls also suggest where to use the below code to test save and reterive the data.
1 $model = Mage::getModel(‘mymod/test’)
2 ->setName(‘Asad Rahman’)
3 ->save();
1 $model = Mage::getModel(‘mymod/test’)
2 $data = $model->getCollection()->getData();
3 print_r($data);
regards
Balvinder
where is setname defined?
If you have a column/ attribute named “name”, in magento model you will automatically (using __calll magic function) have setName & getName function to access that column/attribute value.
Thanks
Are you available for custom magento work? I don’t see a contact email on your site, just a link to your facebook…
Yah … Why not !
Mail me at asad@techigarage.com / asad.dk.bd@gmail.com
Hi,thanks for the tutorial..I want to ask how to use it?Thanks…
Thank you for putting together this tutorial. Worked perfectly! I’m going to use it as a template to write my first module.
Amit,
Great to know it worked for you. Keep in touch.
Hello Asad,
On line 8 of /local/Mypackage/Mymod/model/Mysql4/Test/Collection.php:
Change:
$this->_init(‘Mymod/test’);
To:
$this->_init(‘mymod/test’);
I was having problems using the collection. Thanks for this great article!
Amit
Nice article, very helpful.
Dhonnobad!
Thanks a lot for your article, everything works fine but unable to save the tire_type filed which was newly created.
Please do respond and help me.
Best Regards,
Swathi Chilukuri
I would like to subscribe for this article…
Thank for your post,,,,
please give me another example module related with social networking….
Still waiting for your response…
Let me know exactly what you want to know .
i want to make a module realted to social networking like facebook, twitter in the backend i want to manage user info like user email, no of hits and comments how can we do that can u help me i am just beginner of magento.. please help me .
still waiting for your answer…
Sorry for my delayed response. Please let me know what sort of help you need.
You can mail me at asad.dk.bd@gmail.com . 🙂
how to get the customer details for custom module please give me an example
for example i have users modules i want to manage the customer how we do that please help me on this topic.
Sorry i got the answer…..
thanks for this module.i was searching for good module for a long time…….but i got one problem,when i try to disable from admin side,it does not disable at front end.
Thanks….
hello i m new in Magento and I have to use step by step this.
but It gave 404 page not found error…..
Plz give me solution as soon as possible
Hi Asad ,
Could you help me in finding the problem in my module publisher? I can send it to you if you are interested. I will say more when I get your response.
Hi,
Thanks for the great tutorial. But i was having a problem, i have made IndexController and wrote the following code in it-
$model = Mage::getModel(‘mymod/test’)
->setName(‘Test Test’)
->save();
This code is not inserting any value in test table.
can you please let me out of this trouble?
It’s great. Thanks for the post.
very good tutorial thanks, more details here: http://www.about-magento.com/magento-model-database-tutorial-54
i hope it can help
see you,
Pierre FAY.
hai, i need your help, i add a new link called register on the toplink,and create a form for that also,its work everthing is perfect ,
my question is how i can create a new table in my database for the register form to store the data,
and what are the function are changed for this operation , i new to magento,pls help me
asad bhai you rock!! 🙂 many Thanks!!!