Magento 2 安装数据表

Magento 2 安装数据表 

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

        code:

      <?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。  
      内容如下:

      <?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

      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
  • 完成创建 

猜你喜欢

转载自www.cnblogs.com/q1104460935/p/9458477.html