質問

I've got a module that has this InstallSchema:

<?php
    namespace Vendor\Module\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();

            $intConf = ['unsigned' => true, 'nullable' => false];

            if (!$installer->tableExists('vendor_module_table1')) {
                $table = $installer->getConnection()->newTable($installer->getTable('vendor_module_table1'))
                    ->addColumn(
                        'id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true],
                        'ID'
                    )
                    ->addColumn('grouped_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, $intConf, 'Grouped ID')
                    ->addColumn('simple_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, $intConf, 'Simple ID')
                    ->addColumn('attr_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, $intConf, 'Attribute ID')
                    ->addColumn('value', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, [], 'Value')
                    ->setComment('Table 1');

                $installer->getConnection()->createTable($table);
            }

            if (!$installer->tableExists('vendor_module_table2')) {
                $table = $installer->getConnection()->newTable($installer->getTable('vendor_module_table2'))
                    ->addColumn(
                        'id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true],
                        'ID'
                    )
                    ->addColumn('name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 50, [], 'Name')
                    ->setComment('Table 2');

                $installer->getConnection()->createTable($table);
            }

            if (!$installer->tableExists('vendor_module_table3')) {
                $defaultOpts = ['nullable' => false];

                $table = $installer->getConnection()->newTable($installer->getTable('vendor_module_table3'))
                    ->addColumn(
                        'id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true],
                        'ID'
                    )
                    ->addColumn(
                        'group_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        11,
                        ['nullable' => false, 'unsigned' => true],
                        'Grouped ID'
                    )
                    ->addColumn(
                        'img_number',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        25,
                        $defaultOpts,
                        'Image Number'
                    )
                    ->addColumn(
                        'sku',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        30,
                        $defaultOpts,
                        'SKU'
                    )
                    ->addColumn(
                        'title',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        100,
                        $defaultOpts,
                        'Title'
                    )
                    ->addColumn(
                        'img_name',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        100,
                        $defaultOpts,
                        'Image Name'
                    )
                    ->addColumn(
                        'notes',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        255,
                        $defaultOpts,
                        'Notes'
                    )
                    ->addColumn(
                        'rrp',
                        \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                        null,
                        $defaultOpts,
                        'RRP'
                    )
                    ->addColumn(
                        'required_qty',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        11,
                        $defaultOpts,
                        'Required Qty'
                    )
                    ->addColumn(
                        'availability',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        50,
                        $defaultOpts,
                        'Availability'
                    )
                    ->setComment('Table 3');

                $installer->getConnection()->createTable($table);
            }

            $installer->endSetup();
        }
    }

With this InstallData:

<?php
    namespace Vendor\Module\Setup;

    class InstallData implements \Magento\Framework\Setup\InstallDataInterface
    {
        protected $_tableTwoFactory;

        public function __construct(\Vendor\Module\Model\TableTwoFactory $tableTwoFactory)
        {
            $this->_tableTwoFactory = $tableTwoFactory);
        }

        public function install(
            \Magento\Framework\Setup\ModuleDataSetupInterface $setup,
            \Magento\Framework\Setup\ModuleContextInterface $context
        )
        {
            foreach (['Required Qty', 'Image Number', 'Notes'] as $name)
            {
                $setup->getConnection()->insertForce($setup->getTable('vendor_module_table2'), $name);
            }
        }
    }

This worked when I first created the module and ran the upgrade command. I removed the module and re-installed it, expecting my tables to be made, however they're not.

After some research it seems the common solution is to go into the MySQL database and remove the row from setup_module where module = Module_Name - but that didn't work.

I:

  • removed the code base
  • removed the setup_module table row
  • ran php bin/magento module:disable Vendor_Module
  • ran php bin/magento setup:upgrade
  • ran php bin/magento setup:di:compile
  • ran php bin/magento setup:static-content:deploy -f

I then re-installed the plugin and:

  • ran php bin/magento module:enable Vendor_Module
  • ran php bin/magento setup:upgrade
  • ran php bin/magento setup:di:compile
  • ran php bin/magento setup:static-content:deploy -f

Yet, my tables weren't created. Is there any where, other than the setup_module table that Magento 2 uses to figure out what needs executing, and if so, how do I amend it?

Baseline: How do I get Magento 2 to run my module InstallSchema/Data?

役に立ちましたか?

解決

Hope fully you are missing version checking of the module ,

After $setup->startSetup();

call if (version_compare($context->getVersion(), '1.0.1', '<')) { need to write table schema creation }

Because module version will be registered and checks the data table existence. Module version is taken from

<?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="Namespace_Modulename" setup_version="1.0.5" schema_version="1.0.5">
        <sequence>

        </sequence>
    </module>
</config>

please check , this may help you.

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top