如何将新表添加到magento数据库

时间:2011-01-14 12:08:27

标签: php mysql magento

我正在使用magento 1.4.2.0,我需要在我的magento数据库中添加一个新表,这是我的一些自定义cms页面,我需要将数据添加到数据库并需要获取该数据并显示在cms页面。请一步一步解释我,因为我对magento很新。

3 个答案:

答案 0 :(得分:3)

您必须在模块中使用设置文件:查看Alan Storm's comprehensive post

答案 1 :(得分:0)

使用magento模块创建表在sql文件夹mysql4-install-1.0.0.php文件中编写以下代码。

$installer = $this;
$table = $installer->getConnection()->newTable($installer->getTable(''))
->addColumn('news_id',Varien_Db_Dbl_Table::INTEGER_TYPE,null,array('unsigned'=>true,'identity'=>true,'nullable'=>false,'primary'=>true),'Entity Id')
->addColumn('title',Varien_Db_Dbl_Table::TYPE_TEXT,255,array('nullable'=>true),'Title')
->addColumn('author',Varien_Db_Dbl_Table::TYPE_TEXT,63,array('nullable'=>true,'default'=>'null'),'Author')
->addColumn('content',Varien_Db_Dbl_Table::TYPE_TEXT,'2M',array('nullable'=>true,'default'=>'null'),'Content')
->addColumn('image',Varien_Db_Dbl_Table::TYPE_TEXT,null,array('nullable'=>true,'default'=>'null'),'News Image Media Path')
->addColumn('publised_at',Varien_Db_Dbl_Table::TYPE_DATE,null,array('nullable'=>true,'default'=>'null'),'Published Date')
->addColumn('created_at',Varien_Db_Dbl_Table::TYPE_TIMESTAMP,null,array('nullable'=>true,'default'=>'null'),'Creation Time')
->addIndex($installer->getIdxName($installer->getTable(''),array('publised_at'),Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX),array('publised_at'),array('type'=>Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX))->SetComment('News Item');
$installer->getConnection->createTable($table);

答案 2 :(得分:0)

class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
{

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $table = $setup->getConnection()
        ->newTable($setup->getTable('magento_pincode_pincode'))
        ->addColumn(
                    'pincode_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'nullable' => false,
                        'primary'  => true,
                        'unsigned' => true
                    ],
                    'ID'
                )->addColumn(
                    'pincode',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'Pincode'
                )->addColumn(
                    'status',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    255,
                    ['nullable' => false],
                    'Status'
                )->addColumn(
                    'created_at',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                    null,
                    ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
                    'Created At'
                )->addColumn(
                    'updated_at',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                    null,
                    ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
                    'Updated At');
            $setup->getConnection()->createTable($table);

            $setup->endSetup();

    }
}