0

So I followed this tutorial on how to create a module with a table setup script: http://www.mage-world.com/blog/create-a-module-with-custom-database-table-in-magento-2.html

I took out a few things but here is the code for my InstallSchema file:

<?php
    namespace Company\ExampleAdminNewPage\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();

            $tableName = $installer->getTable('company_exampleadminnewpage');

            //if (!$installer->getConnection()->isTableExists($tableName)) {
                $table = $installer->getConnection()->newTable($tableName);

                $pkOptions = ['identity' => true,  'unsigned' => true, 'nullable' => false, 'primary' => true];

                $tableData = [
                    'pk_id'                 => ['type' => Table::TYPE_INTEGER,  'size' => 11,   'options' => $pkOptions,                            'desc' => 'Dealer Application ID'],
                    'company_name'          => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Company Name'],
                    'trading_start_date'    => ['type' => Table::TYPE_DATETIME, 'size' => null, 'options' => ['nullable' => false],                 'desc' => 'Trading Start Date'],
                    'business_address'      => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Business Address'],
                    'business_address_2'    => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Business Address 2'],
                    'post_code'             => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Post Code'],
                    'telephone'             => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Telephone'],
                    'email'                 => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Email Address'],
                    'website'               => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Website'],
                    'company_status_id'     => ['type' => Table::TYPE_INTEGER,  'size' => 9,    'options' => ['nullable' => false],                 'desc' => 'Company Status ID'],
                    'using_own_afrl'        => ['type' => Table::TYPE_SMALLINT, 'size' => 3,    'options' => ['nullable' => false, 'default' => 0], 'desc' => 'Using Own AFRL System'],
                    'afrl_id_code'          => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => true],                  'desc' => 'AFRL ID Code'],
                    'employee_count'        => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Number of Employees'],
                    'full_time_techs'       => ['type' => Table::TYPE_SMALLINT, 'size' => 3,    'options' => ['nullable' => false],                 'desc' => 'Has Full-Time Technicians'],
                    'delivery_exclude_days' => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Days Not Available for Delivery'],
                    'current_brands'        => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Current Brands'],
                    'avg_sales_per_month'   => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Average Sales Per Month'],
                    'showroom_capacity'     => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Showroom Capacity'],
                    'business_activity'     => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Business Activity'],
                    'application_date'      => ['type' => Table::TYPE_DATETIME, 'size' => null, 'options' => ['nullable' => false],                 'desc' => 'Application Date'],
                    'approved'              => ['type' => Table::TYPE_SMALLINT, 'size' => 3,    'options' => ['nullable' => false, 'default' => 0], 'desc' => 'Approved'],
                    'company_owner_name'    => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Company Owner Name'],
                    'company_owner_dob'     => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => false],                 'desc' => 'Company Owner Date of Birth'],
                    'company_owner2_name'   => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => true],                  'desc' => 'Company Owner 2 Name'],
                    'company_owner2_dob'    => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => true],                  'desc' => 'Company Owner 2 Date of Birth'],
                    'private_address'       => ['type' => Table::TYPE_TEXT,     'size' => 155,  'options' => ['nullable' => true],                  'desc' => 'Private Address']
                ];

                foreach ($tableData as $key => $val)
                {
                    $table->addColumn($key, $val['type'], $val['size'], $val['options'], $val['desc']);
                }

                $table->setComment('Application Table')
                    ->setOption('type', 'InnoDB')
                    ->setOption('charset', 'utf8');
            //}

            $installer->endSetup();
        }
    }

as you can see the only real difference is a lot more columns and using a foreach to loop them instead of manually using ->addColumn per wanted column.

I do this, remove my module from the database table using:

DELETE FROM `setup_module` WHERE `module` = 'Company_ExampleAdminNewPage';

I run the :upgrade, :di:compile and cache:flush (even though all cache is disabled) and go back to the module table to check my module is registered and sure enough I can see it with its correct version number. I run SHOW TABLES; but I can't see my company_exampleadminnewpage table.. I've looked over SO + MSE but haven't been able to find a solution.

I have also tried renaming to upgrade to try an update script but again to no avail. My module works, I can access the hello world in the admin panel, but my installer script isn't firing. I've checked the log files and no entries were made so I'm not sure if my script is working 100%.

Probably something really small but any help would be appreciated.

Thanks :)

0

Try below code:

<?php
namespace Company\ExampleAdminNewPage\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        /**
         * Creating table company_exampleadminnewpage
         */
        $table = $installer->getConnection()->newTable(
            $installer->getTable('company_exampleadminnewpage')
        )->addColumn(
            'pk_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'PK Id'
        )->addColumn(
            'company_name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => true],
            'Company Name'
        );
        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }
}
  • that worked, so I'm guessing there is an issue with my foreach statement? – treyBake Nov 1 '17 at 16:02
  • Great. There might be some issue in your script – Abhishek Panchal Nov 1 '17 at 16:03
  • agreed - though not sure where, can you see anything obvious wrong with my syntax? – treyBake Nov 1 '17 at 16:04
  • Not sure. I never used foreach in my install or upgrade script. – Abhishek Panchal Nov 1 '17 at 16:06
  • I see it. It's the ->createTable() my original script was missing this line, knew it would be something small haha thank you for you help. Will mark as correct as you have shown me my error :) though might be worth editing to say "make sure you include" :) – treyBake Nov 1 '17 at 16:09

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.