-1

我想在我的自定义模块中创建我的自定义表。如何在 Magento 2.3 中创建它。还有其他安装架构的方法吗?我知道magento 1.9。

4

2 回答 2

2

首先,db_schema.xml在里面创建文件/Vendor/Module/etc并编写以下代码:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="table_name" resource="default" engine="innodb" comment="comment_here">
        <column xsi:type="smallint" name="column_name" padding="6" unsigned="false" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="varchar" name="column_name" nullable="false" length="25" comment="Name"/>
        <column xsi:type="varchar" name="column_name" nullable="false" length="25" comment="Email"/>
        <column xsi:type="varchar" name="column_name" nullable="false" length="255" comment="Descrition"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
    </table>
</schema>
  • <table> .. </table>= "用于创建和设置表名"
  • <column> .. </column>= "用于创建和设置表的列"
  • <constraint> .. </constraint>= "用于设置约束,如主键、外键、唯一键等。"

在运行升级命令之前,您需要db_whitelist_schema.json通过运行以下命令将架构添加到文件中:

php bin/magento setup:db-declaration:generate-whitelist --module-name=vendor_module

现在,将在/vendor/module/etc文件夹中创建 db_whitelist_schema.json 文件。

于 2019-03-13T08:48:38.557 回答
1

从 magento 2.3 开始,它们是安装模式的新方法。您必须在 etc/db_schema.xml 中创建新的 xml 文件才能安装架构。

有关更多详细信息,请查看 vendor/magento/module-cms/etc/db_schema.xml 中的文件

于 2019-03-13T04:54:41.033 回答