Magento 1.7 – Add Multiple Custom Tables for Custom Admin Module

admindatabasemagento-1.7magento-1.8module

I have a custom admin mdoule. I need to store the datas of this admin module to 3 different custom tables. I have created admin module with one basic table and successfully stored basic informations into this module. Now I need to configure other two tables to my custom module so that, I can store all my informations related to my module.

I have searched a lot, but couldnt find any tutorials that clearly explain about multiple table configuration. So I have gone through core files and found out these hints.

To set up multiple tables, config.xml file should contain this section

<entities>
     <user>
         <table>admin_user</table>
     </user>
     <role>
         <table>admin_role</table>
     </role>
     <rule>
         <table>admin_rule</table>
     </rule>
     <assert>
         <table>admin_assert</table>
     </assert>
</entities>

Again I have found these codes in sql folder in mysql4-install-0.7.0.php

$installer->run("

 -- DROP TABLE IF EXISTS {$this->getTable('admin_assert')};
 CREATE TABLE {$this->getTable('admin_assert')} (
 `assert_id` int(10) unsigned NOT NULL auto_increment,
  `assert_type` varchar(20) character set utf8 NOT NULL default '',
  `assert_data` text character set utf8,
  PRIMARY KEY  (`assert_id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ACL Asserts';

 -- DROP TABLE IF EXISTS {$this->getTable('admin_role')};
 CREATE TABLE {$this->getTable('admin_role')} (
 `role_id` int(10) unsigned NOT NULL auto_increment,
 `parent_id` int(10) unsigned NOT NULL default '0',
 `tree_level` tinyint(3) unsigned NOT NULL default '0',
 `sort_order` tinyint(3) unsigned NOT NULL default '0',
 `role_type` char(1) character set utf8 NOT NULL default '0',
 `user_id` int(11) unsigned NOT NULL default '0',
 `role_name` varchar(50) character set utf8 NOT NULL default '',
 PRIMARY KEY  (`role_id`),
 KEY `parent_id` (`parent_id`,`sort_order`),
 KEY `tree_level` (`tree_level`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ACL Roles';

insert  into {$this->getTable('admin_role')}   (`role_id`,`parent_id`,`tree_level`,`sort_order`,`role_type`,`user_id`,`role_name`) values (1,0,1,1,'G',0,'Administrators'),(2,1,2,1,'U',1,'Administrator');

-- DROP TABLE IF EXISTS {$this->getTable('admin_rule')};
CREATE TABLE {$this->getTable('admin_rule')} (
 `rule_id` int(10) unsigned NOT NULL auto_increment,
 `role_id` int(10) unsigned NOT NULL default '0',
 `resource_id` varchar(255) character set utf8 NOT NULL default '',
 `privileges` varchar(20) character set utf8 NOT NULL default '',
 `assert_id` int(10) unsigned NOT NULL default '0',
 `role_type` char(1) default NULL,
 `permission` varchar(10) default NULL,
PRIMARY KEY  (`rule_id`),
 KEY `resource` (`resource_id`,`role_id`),
 KEY `role_id` (`role_id`,`resource_id`),
 CONSTRAINT `FK_admin_rule` FOREIGN KEY (`role_id`) REFERENCES {$this->getTable('admin_role')} (`role_id`) ON DELETE CASCADE ON UPDATE CASCADE
 )ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ACL Rules';

insert into {$this->getTable('admin_rule')}(`rule_id`, `role_id`,    `resource_id`, `privileges`, `assert_id`, `role_type`, `permission`) values (1,1,'all','',0,'G','allow');

-- DROP TABLE IF EXISTS {$this->getTable('admin_user')};
CREATE TABLE {$this->getTable('admin_user')} (
  `user_id` mediumint(9) unsigned NOT NULL auto_increment,
  `firstname` varchar(32) character set utf8 NOT NULL default '',
  `lastname` varchar(32) character set utf8 NOT NULL default '',
  `email` varchar(128) character set utf8 NOT NULL default '',
  `username` varchar(40) character set utf8 NOT NULL default '',
  `password` varchar(40) character set utf8 NOT NULL default '',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  `modified` datetime default NULL,
  `logdate` datetime default NULL,
  `lognum` smallint(5) unsigned NOT NULL default '0',
   `reload_acl_flag` tinyint(1) NOT NULL default '0',
  `is_active` tinyint(1) NOT NULL default '1',
   PRIMARY KEY  (`user_id`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Users';
");

Like this, my 3 tables are related each other. Means second and third tables holds first table's id as their parent-reference. I need these three tables available when saveaction takes place. How can I do this. Please provide me hints.. so that I can sort out this problem. Thanks in advance

Best Answer

Take a look at using Data scripts instead of pushing your data in the install/sql. The setup process for them is very similar to the sql/ folder but rather data/ instead. See the SVN code linked.

Also you may want to use Magento's ORM for the inserts and a proper Resource Model. However with that said, if you're not needing to query the data much, you can also use a .sql file instead of .php named in similar fashion. For instance it was a large SQL dump you need to front load with after the tables have been setup, but don't require the need for any PHP execution.

Further Reading:

Related Topic