使用新的声明性模式方法在 Magento 2.3 中创建了一个自定义表,该方法在创建表方面效果很好,除了我不知道如何在创建表时将数据播种到表中。添加数据的方法是否与使用 InstallData.php 不同?
在声明性模式方法之前,它是使用 InstallSchema.php 和 InstallData.php 文件完成的。
我已经尝试将我的 InstallData.php 文件与声明性架构一起使用,因为我无法找到任何地方记录的 InstallData.php 的替换方法(沿着 db_schema.xml 如何替换 InstallSchema.php 的行)。
这是我的 db_schema.xml:
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="sports" resource="default" engine="innodb"
comment="Sports Table">
<column xsi:type="int" name="sport_id" identity="true" unsigned="true" nullable="false" primary="true" comment="Sport Id" />
<column xsi:type="varchar" name="sport_name" length="255" nullable="false" default="" comment="Sport Name" />
<index referenceId="SPORTS_SPORT_ID" indexType="btree">
<column name="sport_id"/>
</index>
</table>
</schema>
这是我的 InstallData.php 的重要部分:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/**
* Install messages
*/
$data = [
['sport_name' => 'Hockey'],
['sport_name' => 'MMA']
];
foreach ($data as $bind) {
$setup->getConnection()
->insertForce($setup->getTable('sports'), $bind);
}
}
我希望该表能够像使用旧方法一样生成填充数据。相反,我让表格显示出来,但里面没有任何数据。