How to create table for custom module in magento?

2020-07-27 20:37发布

问题:

I am new in magento. I need to create my own module (or) extension in Magento. In my module, table cannot be created in the magento table. I am using this code.

File:/app/code/local/com_name/module_name/sql/module_setup/mysql4_install-0.1.0.php
 $installer = $this;
  $installer->startSetup();

  $installer->run("
    DROP TABLE IF EXISTS {$this->getTable('th_tweet')};

    CREATE TABLE {$this->getTable('th_tweet')} (
      `tweet_id` int(11) NOT NULL AUTO_INCREMENT,
      `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `twitter_id` bigint(20) NOT NULL,
      `text` text NOT NULL,
      PRIMARY KEY (`tweet_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1
  ");

  $installer->endSetup();

then, config.xml file can be called like this,

<models>
        <tweet>
            <class>TechAndHouse_Tweet_Model</class>
            <resourceModel>tweet_mysql4</resourceModel>
        </tweet>

            <tweet_mysql4>
                  <class>TechAndHouse_Tweet_Model_Mysql4</class>
                  <entities>
                        <tweet>
                              <table>th_tweet</table>
                        </tweet>
                  </entities>
            </tweet_mysql4>
      </models>

I was creating tabs in admin panel and the front end block was createded but i can not create a table for my custom module. How to create a table for my custom module?

Any help would be appreciated.

回答1:

Add resources section to your config.xml, global

<global>
    ...
    <resources>
        <tweet_setup>
            <setup>
                <module>TechAndHouse_Tweet</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </tweet_setup>
        <tweet_write>
            <connection>
                <use>core_write</use>
            </connection>
        </tweet_write>
        <tweet_read>
            <connection>
                <use>core_read</use>
            </connection>
        </tweet_read>
    </resources>
    ...
</global>