Magento 2 安装数据表
Magento 2 安装数据表
- 第1步:安装脚本
- 首先,我们将为CRUD模型创建数据库表。为此,我们需要插入安装文件
1
app
/code/Mageplaza/HelloWorld/Setup/InstallSchema
.php
code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104<?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。
内容如下:12345<?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.php123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103namespace
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
。
- 首先,我们将为CRUD模型创建数据库表。为此,我们需要插入安装文件
- 完成创建
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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代理技术深度解析与实战指南