zoukankan      html  css  js  c++  java
  • 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 MchelloworldSetup;
        
        class InstallSchema implements MagentoFrameworkSetupInstallSchemaInterface
        {
        
            public function install(MagentoFrameworkSetupSchemaSetupInterface $setup, MagentoFrameworkSetupModuleContextInterface $context)
            {
                $installer = $setup;
                $installer->startSetup();
                if (!$installer->tableExists('mc_helloworld_post')) {
                    $table = $installer->getConnection()->newTable(
                        $installer->getTable('mc_helloworld_post')
                    )
                        ->addColumn(
                            'post_id',
                            MagentoFrameworkDBDdlTable::TYPE_INTEGER,
                            null,
                            [
                                'identity' => true,
                                'nullable' => false,
                                'primary'  => true,
                                'unsigned' => true,
                            ],
                            'Post ID'
                        )
                        ->addColumn(
                            'name',
                            MagentoFrameworkDBDdlTable::TYPE_TEXT,
                            255,
                            ['nullable => false'],
                            'Post Name'
                        )
                        ->addColumn(
                            'url_key',
                            MagentoFrameworkDBDdlTable::TYPE_TEXT,
                            255,
                            [],
                            'Post URL Key'
                        )
                        ->addColumn(
                            'post_content',
                            MagentoFrameworkDBDdlTable::TYPE_TEXT,
                            '64k',
                            [],
                            'Post Post Content'
                        )
                        ->addColumn(
                            'tags',
                            MagentoFrameworkDBDdlTable::TYPE_TEXT,
                            255,
                            [],
                            'Post Tags'
                        )
                        ->addColumn(
                            'status',
                            MagentoFrameworkDBDdlTable::TYPE_INTEGER,
                            1,
                            [],
                            'Post Status'
                        )
                        ->addColumn(
                            'featured_image',
                            MagentoFrameworkDBDdlTable::TYPE_TEXT,
                            255,
                            [],
                            'Post Featured Image'
                        )
                        ->addColumn(
                            'created_at',
                            MagentoFrameworkDBDdlTable::TYPE_TIMESTAMP,
                            null,
                            ['nullable' => false, 'default' => MagentoFrameworkDBDdlTable::TIMESTAMP_INIT],
                            'Created At'
                        )->addColumn(
                            'updated_at',
                            MagentoFrameworkDBDdlTable::TYPE_TIMESTAMP,
                            null,
                            ['nullable' => false, 'default' => MagentoFrameworkDBDdlTable::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'],
                            MagentoFrameworkDBAdapterAdapterInterface::INDEX_TYPE_FULLTEXT
                        ),
                        ['name','url_key','post_content','tags','featured_image'],
                        MagentoFrameworkDBAdapterAdapterInterface::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 mcHelloWorldSetup;
        
        use MagentoFrameworkSetupUpgradeSchemaInterface;
        use MagentoFrameworkSetupSchemaSetupInterface;
        use MagentoFrameworkSetupModuleContextInterface;
        
        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',
                                MagentoFrameworkDBDdlTable::TYPE_INTEGER,
                                null,
                                [
                                    'identity' => true,
                                    'nullable' => false,
                                    'primary'  => true,
                                    'unsigned' => true,
                                ],
                                'Post ID'
                            )
                            ->addColumn(
                                'name',
                                MagentoFrameworkDBDdlTable::TYPE_TEXT,
                                255,
                                ['nullable => false'],
                                'Post Name'
                            )
                            ->addColumn(
                                'url_key',
                                MagentoFrameworkDBDdlTable::TYPE_TEXT,
                                255,
                                [],
                                'Post URL Key'
                            )
                            ->addColumn(
                                'post_content',
                                MagentoFrameworkDBDdlTable::TYPE_TEXT,
                                '64k',
                                [],
                                'Post Post Content'
                            )
                            ->addColumn(
                                'tags',
                                MagentoFrameworkDBDdlTable::TYPE_TEXT,
                                255,
                                [],
                                'Post Tags'
                            )
                            ->addColumn(
                                'status',
                                MagentoFrameworkDBDdlTable::TYPE_INTEGER,
                                1,
                                [],
                                'Post Status'
                            )
                            ->addColumn(
                                'featured_image',
                                MagentoFrameworkDBDdlTable::TYPE_TEXT,
                                255,
                                [],
                                'Post Featured Image'
                            )
                            ->addColumn(
                                'created_at',
                                MagentoFrameworkDBDdlTable::TYPE_TIMESTAMP,
                                null,
                                ['nullable' => false, 'default' => MagentoFrameworkDBDdlTable::TIMESTAMP_INIT],
                                'Created At'
                            )->addColumn(
                                'updated_at',
                                MagentoFrameworkDBDdlTable::TYPE_TIMESTAMP,
                                null,
                                ['nullable' => false, 'default' => MagentoFrameworkDBDdlTable::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'],
                                MagentoFrameworkDBAdapterAdapterInterface::INDEX_TYPE_FULLTEXT
                            ),
                            ['name','url_key','post_content','tags','featured_image'],
                            MagentoFrameworkDBAdapterAdapterInterface::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
    • 完成创建 
  • 相关阅读:
    关于MATLAB收集人工鼠标移动轨迹的坐标
    对交叉验证的认识
    关于MATLAB处理大数据坐标文件2017624
    关于MATLAB处理大数据坐标文件2017622
    关于MATLAB处理大数据坐标文件2017620
    关于MATLAB处理大数据坐标文件201763
    关于MATLAB处理大数据坐标文件201762
    关于MATLAB处理大数据坐标文件201761
    关于MATLAB处理大数据坐标文件2017530
    [leetcode] 70. Climbing Stairs 解题报告
  • 原文地址:https://www.cnblogs.com/q1104460935/p/9458477.html
Copyright © 2011-2022 走看看