InstallSchema
, or install script, is a way for us to set up tables in the database that will be used to persist our models later on.
If we look back at the module requirements, the following fields need to be created in the foggyline_helpdesk_ticket
table:
ticket_id
customer_id
title
severity
created_at
status
Our InstallSchema
is defined under the app/code/Foggyline/Helpdesk/Setup/InstallSchema.php
file with (partial) content as follows:
<?php namespace FoggylineHelpdeskSetup; use MagentoFrameworkSetupInstallSchemaInterface; use MagentoFrameworkSetupModuleContextInterface; use MagentoFrameworkSetupSchemaSetupInterface; /** * @codeCoverageIgnore */ class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer-> getTable('foggyline_helpdesk_ticket')) /* ->addColumn ... */ /* ->addIndex ... */ /* ->addForeignKey ... */ ->setComment('Foggyline Helpdesk Ticket'); $installer->getConnection()->createTable($table); $installer->endSetup(); } }
The InstallSchema
class conforms to InstallSchemaInterface
by implementing a single install
method. Within this method, we start the installer, create new tables, create new fields, add indexes and foreign keys to the table, and finally end the installer, as shown in the following (partial) code:
->addColumn( 'ticket_id', MagentoFrameworkDBDdlTable::TYPE_INTEGER, null, ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], 'Ticket Id' ) ->addColumn( 'customer_id', MagentoFrameworkDBDdlTable::TYPE_INTEGER, null, ['unsigned' => true], 'Customer Id' ) ->addColumn( 'title', MagentoFrameworkDBDdlTable::TYPE_TEXT, null, ['nullable' => false], 'Title' ) ->addColumn( 'severity', MagentoFrameworkDBDdlTable::TYPE_SMALLINT, null, ['nullable' => false], 'Severity' ) ->addColumn( 'created_at', MagentoFrameworkDBDdlTable::TYPE_TIMESTAMP, null, ['nullable' => false], 'Created At' ) ->addColumn( 'status', MagentoFrameworkDBDdlTable::TYPE_SMALLINT, null, ['nullable' => false], 'Status' ) ->addIndex( $installer->getIdxName('foggyline_helpdesk_ticket', ['customer_id']), ['customer_id'] ) ->addForeignKey( $installer->getFkName('foggyline_helpdesk_ticket', 'customer_id', 'customer_entity', 'entity_id'), 'customer_id', $installer->getTable('customer_entity'), 'entity_id', MagentoFrameworkDBDdlTable::ACTION_SET_NULL )
The provided code shows each of the fields from the module requirement being added to the database using the addColumn
method call and passing it certain parameters such as the field type
and nullable
state. It is worth getting familiar with the addColumn
, addIndex
, and addForeignKey
methods as these are most commonly used when specifying new tables for our modules.
3.133.131.168