3

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.

3

I think this is the problem

if (version_compare($context->getVersion(), '1.0.1') == 0) {

it should be

if (version_compare($context->getVersion(), '1.0.1') < 0) {

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.

  • 6
    You can even use the third parameter of version_compare: (version_compare($context->getVersion(), '1.0.1','<') – Raphael at Digital Pianism Feb 11 '16 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.