本篇文章更新至:2019年03月26日
今天我想讲讲magento 2的数据库更新表或者表结构是怎么改变的。有时候我们在进行开发magento的时候,我们需要进行添加表和子段,magento 2给我们一种很好的创建数据库表的
程序 ,不需要进入数据库里面创建表。下面我将讲述怎么在代码里面创建数据库表以及表结构:
第一步: 在Setup文件夹下创建InstallSchema.php以下代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?php namespace Hello\Smtp\Setup; use Magento\Framework\DB\Ddl\Table; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; /** * Class InstallSchema * @package hello\Smtp\Setup */ class InstallSchema implements InstallSchemaInterface { /** * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context * @throws \Zend_Db_Exception */ public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); if (!$installer->tableExists('hello_smtp_log')) { $table = $installer->getConnection() ->newTable($installer->getTable('hello_smtp_log')) ->addColumn('id', Table::TYPE_INTEGER, null, [ 'identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true, ], 'Log ID') ->addColumn('subject', Table::TYPE_TEXT, 255, ['nullable => false'], 'Email Subject') ->addColumn('email_content', Table::TYPE_TEXT, '64k', [], 'Email Content') ->addColumn('status', Table::TYPE_SMALLINT, 1, ['nullable' => false], 'Status') ->addColumn('created_at', Table::TYPE_TIMESTAMP, null, [], 'Created At') ->addIndex($installer->getIdxName('hello_smtp_log', ['status']), ['status']); $installer->getConnection()->createTable($table); } $installer->endSetup(); } } |
第2步:更改etc文件夹下module.xml代码如下 setup_version=”1.1.1″
第3步:运行php bin/magento setup:upgrade 的CLI 运行命令
如果要已经安装了数据表和结构,有时候需要添加或者删除表的子段,这个时候就要使用更新表结构了。
第4步:在Setup文件夹下创建UpgradeSchema.php以下代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php namespace Hello\Smtp\Setup; use Magento\Framework\DB\Ddl\Table; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\UpgradeSchemaInterface; /** * Class UpgradeSchema * @package hello\Smtp\Setup */ class UpgradeSchema implements UpgradeSchemaInterface { /** * @param \Magento\Framework\Setup\SchemaSetupInterface $setup * @param \Magento\Framework\Setup\ModuleContextInterface $context */ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $connection = $setup->getConnection(); if (version_compare($context->getVersion(), '1.1.0', '<')) { $connection->addColumn($setup->getTable('hello_smtp_log'), 'from', [ 'type' => Table::TYPE_TEXT, 'nullable' => true, 'length' => 255, 'comment' => 'Sender' ]); $connection->addColumn($setup->getTable('hello_smtp_log'), 'to', [ 'type' => Table::TYPE_TEXT, 'nullable' => true, 'length' => 255, 'comment' => 'Recipient' ]); $connection->addColumn($setup->getTable('hello_smtp_log'), 'cc', [ 'type' => Table::TYPE_TEXT, 'nullable' => true, 'length' => 255, 'comment' => 'Cc' ]); $connection->addColumn($setup->getTable('hello_smtp_log'), 'bcc', [ 'type' => Table::TYPE_TEXT, 'nullable' => true, 'length' => 255, 'comment' => 'Bcc' ]); } if (version_compare($context->getVersion(), '1.1.1', '<')) { $connection->changeColumn($setup->getTable('hello_smtp_log'), 'from', 'sender', [ 'type' => Table::TYPE_TEXT, 'nullable' => true, 'length' => 255, 'comment' => 'Sender' ]); $connection->changeColumn($setup->getTable('hello_smtp_log'), 'to', 'recipient', [ 'type' => Table::TYPE_TEXT, 'nullable' => true, 'length' => 255, 'comment' => 'Recipient' ]); } $setup->endSetup(); } } |
第5步:更改etc文件夹下module.xml代码如下 setup_version=”1.1.2″
第6步:在次运行php bin/magento setup:upgrade 的CLI 运行命令
如果对你有帮助请留言谢谢。希望对你有帮助。
文章不错支持一下吧
文章不错非常喜欢
写的很好,很喜欢