1

While developing a new extension, I'm trying to update two tables at installation time by using Setup\InstallSchema.php. The tables are "admin_user" and "customer_entity", and I basically need the same fields for both of them.

The InstallSchema.php is triggered, but the tables don't get updated.

Please see the code & info below:

namespace Vendor\ModuleName\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
{
    protected $installer;

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->installer = $setup;
        $this->installer->startSetup();

        $this->addUsersFields('admin_user');
        $this->addUsersFields('customer_entity');

        $this->installer->endSetup();
    }

    protected function addUsersFields($table)
    {
        $table = $this->installer->getTable($table);

        $this->installer->getConnection()->addColumn(
            $table,
            'field1',
            [
                'type' => Table::TYPE_TEXT,
                'nullable' => false,
                'default' => 'field1Default',
                'length' => '45',
                'comment' => 'comment'
            ]
        );

        $this->installer->getConnection()->addColumn(
            $table,
            'field2',
            [
                'type' => Table::TYPE_TEXT,
                'nullable' => false,
                'default' => 'field2Default',
                'length' => '45',
                'comment' => 'comment'
            ]
        );
    }
}

EDITED: updated fields name correctly : field1 and field2

Using Magento 2.2.2 on AWS, from an AWS AMI. Developer mode enabled.

I'm running :

  1. php bin/magento module:enable ModuleName (works fine, tells me that has enabled the module)
  2. php bin/magento setup:upgrade (works fine, I can see the module in the output)
  3. php bin/magento setup:di:compile (works fine)

I've :

  1. made sure the InstallSchema.php is included each time by deleting the module entry from "setup_module" table;
  2. tested that it's triggered by deliberately deleting an ";" from a line, and within php bin/magento setup:di:compile it shows an error message and the process is stopped.

So basically I'm 100% sure the InstallSchema.php is triggered.

THE PROBLEM is that the tables don't get updated, nor do I get any error message. Neither anything worth mentioned in exception.log, system.log or /var/log/apache2/error.log.

Any way to debug this?

0

You need do add installer in passsing parameter instead of table.

protected function addUsersFields($installer){}

and get table name code like:

$tableName = $installer->getTable('tablename');
  • the installer is already set: $this->installer = $setup; – Florin Popescu Dec 21 '17 at 14:18
0

You have included field1 two times in the addUsersFields function while adding column in addColumn 2nd argument. Lets change it to field2 then check the table...

protected function addUsersFields($table)
{
    $table = $this->installer->getTable($table);

    $this->installer->getConnection()->addColumn(
        $table,
        'field1',
        [
            'type' => Table::TYPE_TEXT,
            'nullable' => false,
            'default' => 'field1Default',
            'length' => '45',
            'comment' => 'comment'
        ]
    );

    $this->installer->getConnection()->addColumn(
        $table,
        'field2',
        [
            'type' => Table::TYPE_TEXT,
            'nullable' => false,
            'default' => 'field2Default',
            'length' => '45',
            'comment' => 'comment'
        ]
    );
}
  • No no, that's just a typo I've made here. field1 and field2 you see here, are just bogus field names. In the original script, all the fields are different and their name written correctly. – Florin Popescu Dec 21 '17 at 14:58
  • did you check error log? and apache log? – Ashish Raj Dec 21 '17 at 15:02
  • I've monitored exception.log, system.log and /var/log/apache2/error.log . Nothing. – Florin Popescu Dec 21 '17 at 15:28

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.