Magento model to interact with db table
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.
1 |
CREATE TABLE `test` ( |
2 |
`test_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , |
3 |
` name ` VARCHAR ( 25 ) NOT NULL |
4 |
) ENGINE = MYISAM |
2.Create shell module Mymod under package Mypackage
//file:- /app/etc/modules/Mypackage_Mymod.xml
1 |
<? xml version = "1.0" ?> |
2 |
< config > |
3 |
< modules > |
4 |
< Mypackage_Mymod > |
5 |
< active >true</ active > |
6 |
< codePool >local</ codePool > |
7 |
</ Mypackage_Mymod > |
8 |
</ modules > |
9 |
</ 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
01 |
/ |
02 |
<? xml version = "1.0" ?> |
03 |
< config > |
04 |
< modules > |
05 |
< Mypackage_Mymod > |
06 |
< version >0.1.0</ version > |
07 |
</ Mypackage_Mymod > |
08 |
</ modules > |
09 |
< global > |
10 |
< models > |
11 |
< mymod > |
12 |
<!-- Init model for mymod module --> |
13 |
< class >Mypackage_Mymod_Model</ class > |
14 |
<!-- Init db config handler for mymod models --> |
15 |
< resourceModel >mymod_mysql4</ resourceModel > |
16 |
</ mymod > |
17 |
18 |
<!-- declaring model vs db table relation --> |
19 |
< mymod_mysql4 > |
20 |
< class >Mypackage_Mymod_Model_Mysql4</ class > |
21 |
<!-- declate table test --> |
22 |
< entities > |
23 |
< test > |
24 |
< table >test</ table > |
25 |
</ test > |
26 |
</ entities > |
27 |
<!-- -/- --> |
28 |
</ mymod_mysql4 > |
29 |
<!-- -/- --> |
30 |
</ models > |
31 |
32 |
<!-- Setup db read & write connection for Mymod module --> |
33 |
< resources > |
34 |
<!-- db write connection --> |
35 |
< mymod_write > |
36 |
< connection > |
37 |
< use >core_write</ use > |
38 |
</ connection > |
39 |
</ mymod_write > |
40 |
<!-- db read connection --> |
41 |
< mymod_read > |
42 |
< connection > |
43 |
< use >core_read</ use > |
44 |
</ connection > |
45 |
</ mymod_read > |
46 |
</ resources > |
47 |
<!-- -/- --> |
48 |
</ global > |
49 |
</ 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
01 |
<?php |
02 |
03 |
class Mypackage_Mymod_Model_Test extends Mage_Core_Model_Abstract |
04 |
{ |
05 |
06 |
public function _construct() |
07 |
{ |
08 |
parent::_construct(); |
09 |
$this ->_init( 'mymod/test' ); |
10 |
} |
11 |
} |
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.
1 |
<?php |
2 |
3 |
class Mypackage_Mymod_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract |
4 |
{ |
5 |
public function _construct() |
6 |
{ |
7 |
$this ->_init( 'mymod/test' , 'test_id' ); |
8 |
} |
9 |
} |
6.Create a collection class so that we can retrive data from table test.
file:-/local/Mypackage/Mymod/model/Mysql4/Test/Collection.php
01 |
<?php |
02 |
03 |
class Mypackage_Mymod_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract |
04 |
{ |
05 |
public function _construct() |
06 |
{ |
07 |
parent::_construct(); |
08 |
$this ->_init( 'Mymod/test' ); |
09 |
} |
10 |
} |
That’s it we are done. Now we can do AMD operation with table test with model test.
1 |
$model = Mage::getModel( 'mymod/test' ) |
2 |
->setName( 'Asad Rahman' ) |
3 |
->save(); |
Also use the test collection model to retrive collection data from table test.
1 |
$model = Mage::getModel( 'mymod/test' ) |
2 |
$data = $model ->getCollection()->getData(); |
3 |
print_r( $data ); |