I made a simple module wich create a simple table.
This is my module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Tutorial_TutorialTable" setup_version="1.0.0" active="true">
</module>
</config>
This is my InstallSchema.php under Tutorial/TutorialTable/Setup/
<?php
namespace Tutorial\TutorialTable\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
// Get tutorial_simplenews table
$tableName = $installer->getTable('tutorial_simplenews');
// Check if the table already exists
if ($installer->getConnection()->isTableExists($tableName) != true) {
// Create tutorial_simplenews table
$table = $installer->getConnection()
->newTable($tableName)
->addColumn(
'id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'ID'
)
and so on…
The table is correctly created.
Now I want to use UpgradeData.php to insert into table something and I add UpgradeData.php under Tutorial/TutorialTable/Setup
<?php
namespace Tutorial\TutorialTable\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class UpgradeData implements UpgradeDataInterface
{
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1') == 0) {
// Get tutorial_simplenews table
$tableName = $setup->getTable('tutorial_simplenews');
// Check if the table already exists
if ($setup->getConnection()->isTableExists($tableName) == true) {
.. add some data in $data array .. and then
foreach ($data as $item) {
$setup->getConnection()->insert($tableName, $item);
}
}
}
$setup->endSetup();
}
}
Si i edit the setup_version in my module.xml
setup_version="1.0.1"
and then i run
php bin/magento setup:upgrade
And all module is correctly load but i don't have any data in my table.
Where i'm wrong?
Thanks in advice.
Best Answer
I think this is the problem
it should be
because when you run the upgradeData file the version is still
1.0.0
and it becomes 1.0.1 only after the upgrade is done.