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

Magento 2 安装数据表的更多相关文章

  1. mysql笔记1—安装、配置和基础的数据表操作

    本篇笔记主要分为两部分: 1,安装完毕之后的简单配置 2,数据的类型.简单的数据表操作命令 一.mysql安装完毕之后 windows和linux环境,除mysql的安装.配置有所不同,其他操作一样, ...

  2. 改用C++生成自动化数据表

    改用C++生成自动化数据表 前面的文章中,我们讨论了使用一个基于.NET的第三方程序库来从程序中来生成数据表.在我看来,这整个思路是非常有用的,例如为显示测试结果.我经常会自己在博客中尝试各种像这样的 ...

  3. 使用Entity Framework通过code first方式创建数据库和数据表

    开发环境 WIN10 Entity Framework6.0  MVC5.0  开发工具 VS2015  SqlServer2012 1.创建上下文Context继承DbContext,并创建其他的业 ...

  4. MySQL的数据库,数据表,数据的操作

    数据库简介 概念 什么是数据库?简单来说,数据库就是存储数据的"仓库", 但是,光有数据还不行,还要管理数据的工具,我们称之为数据库管理系统! 数据库系统 = 数据库管理系统 + ...

  5. python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API

    python  Django教程  之 模型(数据库).自定义Field.数据表更改.QuerySet API 一.Django 模型(数据库) Django 模型是与数据库相关的,与数据库相关的代码 ...

  6. ubuntu下面mysql,通过载入txt文件初始化数据表

    环境:ubuntu12.04   mysql(通过apt安装) (1)根据数据表中的属性列,对应在txt中构造记录(一行对应一条记录),不同属性之间通过tab键(以/root目录下构建的init.tx ...

  7. MySQL(一) 数据表数据库的基本操作

    序言 这类文章,记录我看<MySQL5.6从零开始学>这本书的过程,将自己觉得重要的东西记录一下,并有可能帮助到你们,在写的博文前几篇度会非常基础,只要动手敲,跟着我写的例子全部实现一遍, ...

  8. 在Openfire中使用自己的数据表之修改配置文件

    目前我使用的Openfire版本是3.10.3,以下使用说明也是在这个版本上做的修改. Openfire提供了两种方式使用用户数据表.一种是安装完成之后默认实现的org.jivesoftware.op ...

  9. 用SQLSERVER里的bcp命令或者bulkinsert命令也可以把dat文件导入数据表

    用SQLSERVER里的bcp命令或者bulkinsert命令也可以把dat文件导入数据表 下面的内容的实验环境我是在SQLSERVER2005上面做的 之前在园子里看到两篇文章<C# 读取纯真 ...

随机推荐

  1. SAP MM 无价值物料管理的一种实现思路

    SAP MM 无价值物料管理的一种实现思路 笔者所在的项目,客户工厂处于先期试生产阶段,尚未开始大规模的商业化生产,但是这并不影响客户集团总部的SAP项目实施.笔者于7月初加入该工厂的第2期SAP项目 ...

  2. AndroisStudio列选择模式

    今天敲代码的时候可能由于误开了“列选择模式”,在移动光标时,发现若光标所在列超过当前行的行尾列,不像一般情况应该跳到行尾,而变成了保持列的位置,停在了超过行尾的空白处, 如图:非一般情况 一般情况: ...

  3. Tips on GORM, Avoid Error about "duplicate column name: id"

    The GORM is an super easy ORM solution for Go language. But many people would get the error about du ...

  4. 基于WanAndroid开放API实现的文章阅读APP

    简介 基于WanAndroid开放API开发的技术文章阅读App.主要功能包括:首页.体系.项目.公众号.搜索.登录.收藏.夜间模式等. 用到的第三方框架 RxJava RxAndroid Retro ...

  5. elasticsearch常用命令

    elasticsearch的rest访问格式: curl -X<REST Verb> <Node>:<Port>/<Index>/<Type> ...

  6. 部署 Prometheus Operator - 每天5分钟玩转 Docker 容器技术(179)

    本节在实践时使用的是 Prometheus Operator 版本 v0.14.0.由于项目开发迭代速度很快,部署方法可能会更新,必要时请参考官方文档. 下载最新源码 git clone https: ...

  7. web渗透 学习计划(转载)

    作者:向生李链接:https://www.zhihu.com/question/21914899/answer/39344435来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  8. LeetCode算法题-Minimum Absolute Difference in BST(Java实现)

    这是悦乐书的第253次更新,第266篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第120题(顺位题号是530).给定具有非负值的二叉搜索树,找到任意两个节点的值之间的最 ...

  9. 2017 百度杯丶春秋欢乐赛 writeup

    1. 内涵图(Misc) 题目: 我不是一个简单的图片 我是一个有内涵的图片 解:保存到桌面,右键属性->详细信息,即可获得flag. 2. 小电影(Misc) 题目: 我说过 这次比赛是让大家 ...

  10. Django REST framework基础:解析器和渲染器

    解析器 解析器的作用 解析器的作用就是服务端接收客户端传过来的数据,把数据解析成自己可以处理的数据.本质就是对请求体中的数据进行解析. 在了解解析器之前,我们要先知道Accept以及ContentTy ...