Magento 2 安装数据表

Magento 2 安装数据表 

  • 第1步:安装脚本
    •  首先,我们将为CRUD模型创建数据库表。为此,我们需要插入安装文件
      1
      app/code/Mageplaza/HelloWorld/Setup/InstallSchema.php

        code:

      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
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      <?php
      /**
       * Created by PhpStorm.
       * User: jerryxu
       * Date: 2018/8/11
       * Time: 上午12:22
       */
       
      namespace Mc\helloworld\Setup;
       
      class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
      {
       
          public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
          {
              $installer = $setup;
              $installer->startSetup();
              if (!$installer->tableExists('mc_helloworld_post')) {
                  $table = $installer->getConnection()->newTable(
                      $installer->getTable('mc_helloworld_post')
                  )
                      ->addColumn(
                          'post_id',
                          \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                          null,
                          [
                              'identity' => true,
                              'nullable' => false,
                              'primary'  => true,
                              'unsigned' => true,
                          ],
                          'Post ID'
                      )
                      ->addColumn(
                          'name',
                          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                          255,
                          ['nullable => false'],
                          'Post Name'
                      )
                      ->addColumn(
                          'url_key',
                          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                          255,
                          [],
                          'Post URL Key'
                      )
                      ->addColumn(
                          'post_content',
                          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                          '64k',
                          [],
                          'Post Post Content'
                      )
                      ->addColumn(
                          'tags',
                          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                          255,
                          [],
                          'Post Tags'
                      )
                      ->addColumn(
                          'status',
                          \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                          1,
                          [],
                          'Post Status'
                      )
                      ->addColumn(
                          'featured_image',
                          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                          255,
                          [],
                          'Post Featured Image'
                      )
                      ->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')
                      ->setComment('Post Table');
                  $installer->getConnection()->createTable($table);
       
                  $installer->getConnection()->addIndex(
                      $installer->getTable('mc_helloworld_post'),
                      $setup->getIdxName(
                          $installer->getTable('mc_helloworld_post'),
                          ['name','url_key','post_content','tags','featured_image'],
                          \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
                      ),
                      ['name','url_key','post_content','tags','featured_image'],
                      \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
                  );
              }
              $installer->endSetup();
          }
      }

       

    • 请注意,Magento将在安装模块时首次自动运行此文件。如果之前安装了模块,则需要升级模块并将表创建代码写入该文件夹中的UpgradeSchema.php,并将属性更改为setup_version大于当前安装版本的module.xmlat app/code/Mageplaza/HelloWorld/etc/module.xml。  
      内容如下:

      1
      2
      3
      4
      5
      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
          <module name="Mc_Mysize" setup_version="1.1.0">
          </module>
      </config>

      module.xml文件中,我们改变了属性1.1.0大于setup_version
      文件: app/code/Mageplaza/HelloWorld/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
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      namespace mc\HelloWorld\Setup;
       
      use Magento\Framework\Setup\UpgradeSchemaInterface;
      use Magento\Framework\Setup\SchemaSetupInterface;
      use Magento\Framework\Setup\ModuleContextInterface;
       
      class UpgradeSchema implements UpgradeSchemaInterface
      {
          public function upgrade( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
              $installer = $setup;
       
              $installer->startSetup();
       
              if(version_compare($context->getVersion(), '1.1.0', '<')) {
                  if (!$installer->tableExists('mc_helloworld_post')) {
                      $table = $installer->getConnection()->newTable(
                          $installer->getTable('mc_helloworld_post')
                      )
                          ->addColumn(
                              'post_id',
                              \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                              null,
                              [
                                  'identity' => true,
                                  'nullable' => false,
                                  'primary'  => true,
                                  'unsigned' => true,
                              ],
                              'Post ID'
                          )
                          ->addColumn(
                              'name',
                              \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                              255,
                              ['nullable => false'],
                              'Post Name'
                          )
                          ->addColumn(
                              'url_key',
                              \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                              255,
                              [],
                              'Post URL Key'
                          )
                          ->addColumn(
                              'post_content',
                              \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                              '64k',
                              [],
                              'Post Post Content'
                          )
                          ->addColumn(
                              'tags',
                              \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                              255,
                              [],
                              'Post Tags'
                          )
                          ->addColumn(
                              'status',
                              \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                              1,
                              [],
                              'Post Status'
                          )
                          ->addColumn(
                              'featured_image',
                              \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                              255,
                              [],
                              'Post Featured Image'
                          )
                          ->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')
                          ->setComment('Post Table');
                      $installer->getConnection()->createTable($table);
       
                      $installer->getConnection()->addIndex(
                          $installer->getTable('mc_helloworld_post'),
                          $setup->getIdxName(
                              $installer->getTable('mc_helloworld_post'),
                              ['name','url_key','post_content','tags','featured_image'],
                              \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
                          ),
                          ['name','url_key','post_content','tags','featured_image'],
                          \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
                      );
                  }
              }
       
              $installer->endSetup();
          }
      }

        在此之后请运行此命令行:

    • php bin/magento setup:upgrade && php bin/magento setup:static-content:deploy -f
    • InstallSchema.php 用于创建数据库结构。如果要将数据安装到创建的表中,则需要使用 app/code/Mageplaza/HelloWorld/Setup/InstallData.php

      请查看Magento中的一些InstallData文件,了解如何使用它。
      - vendor/magento/module-tax/Setup/InstallData.php
      - vendor/magento/module-customer/Setup/InstallData.php
      - vendor/magento/module-catalog/Setup/InstallData.php
    • 如上所述,那些安装文件将用于第一次安装模块。如果要在升级模块时更改数据库,请尝试使用UpgradeSchema.php 和  UpgradeData.php
  • 完成创建 

     

 

posted @   徐锅  阅读(345)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

点击右上角即可分享
微信分享提示