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 :)