Magento 1.8 Table – Best Way of Creating a New Table in Magento

createinstall-scriptmagento-1.8table

I have seen two different ways of creating a new table in Magento. I want to figure out which one is the best? And why there are two ways? What are the differences between them?

First way

(Reference : http://inchoo.net/ecommerce/magento/magento-install-install-upgrade-data-and-data-upgrade-scripts/comment-page-1/#comment-379299)

$installer = $this;
$installer->startSetup();
$table = $installer->getConnection()
    ->newTable($installer->getTable('inchoo_dbscript/ticket'))
    ->addColumn('ticket_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Id')
    ->addColumn('title', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
        'nullable'  => false,
        ), 'Title')
    ->addColumn('description', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable'  => false,
        ), 'Description');
$installer->getConnection()->createTable($table);
$table = $installer->getConnection()
    ->newTable($installer->getTable('inchoo_dbscript/comment'))
    ->addColumn('comment_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Id')
    ->addColumn('comment', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
        'nullable'  => false,
        ), 'Comment');
$installer->getConnection()->createTable($table);
$installer->endSetup();

Second way

(Reference : http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table)

<?php

$installer = $this;

$installer->startSetup();

$installer->run("

-- DROP TABLE IF EXISTS {$this->getTable('<module>')};
CREATE TABLE {$this->getTable('<module>')} (
  `<module>_id` int(11) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL default '',
  `content` text NOT NULL default '',
  `status` smallint(6) NOT NULL default '0',
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  PRIMARY KEY (`<module>_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ");

$installer->endSetup();

Best Answer

Both methods have the same result.
The first one you mentioned was introduces in CE-1.6.
In theory that one is the "more correct" than the second one you mentioned second.
Let's say that through some magic Magento supports (natively) other db engine than MySQL.
If you use the first approach in your extensions you won't have to do anything. The extension will work on the other db engine because there should be a driver that transforms the DDL declaration in the query needed for that specific engine.

If you use the second one you might need to write an other install/update script to fit your new db engine.

If you want to be politically correct you should use the first one, but if you don't plan to use something else other than MySQL you shouldn't care.

Related Topic