The InstallSchema script

The InstallSchema script is used when we wish to add new columns to existing tables or create new tables. This script is run only when a module is enabled. Once enabled, the module gets a corresponding entry under the setup_module.schema_version table column. This entry prevents the InstallSchema script running on any subsequent setup:upgrade command where the module's setup_version remains the same.

Let's go ahead and create the <MAGELICIOUS_DIR>/Core/Setup/InstallSchema.php file with the following content:

use MagentoFrameworkSetupInstallSchemaInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupSchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface {
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
echo 'InstallSchema->install()' . PHP_EOL;
$setup->endSetup();
}
}

The use of $setup->startSetup(); and $setup->endSetup(); is a common practice among the majority of setup scripts. The implementation of these two methods deals with running additional environment setup steps, such as setting SQL_MODE and FOREIGN_KEY_CHECKS, as can be seen under MagentoFrameworkDBAdapterPdoMysql.

To make something useful out of it, let's go ahead and replace the echo line with the code that actually creates our magelicious_core_log table:

$table = $setup->getConnection()
->newTable($setup->getTable('magelicious_core_log'))
->addColumn(
'entity_id',
MagentoFrameworkDBDdlTable::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Entity ID'
)->addColumn(
'severity_level',
MagentoFrameworkDBDdlTable::TYPE_TEXT,
24,
['nullable' => false],
'Severity Level'
)->addColumn(
'note',
MagentoFrameworkDBDdlTable::TYPE_TEXT,
null,
['nullable' => false],
'Note'
)->addColumn(
'created_at',
MagentoFrameworkDBDdlTable::TYPE_TIMESTAMP,
null,
['nullable' => false],
'Created At'
)->setComment('Magelicious Core Log Table'),
$setup->getConnection()->createTable($table);

$setup->getConnection() gets us the database adapter instance. From there on, we get access to methods that are needed for database table creation. When it comes to InstallSchema scripts, the majority of the time, the following methods will do the job:

  • newTable: Retrieves a DDL object for the new table
  • addColumn: Adds columns to the table
  • addIndex: Adds an index to the table
  • addForeignKey: Adds a foreign key to the table
  • setComment: Sets a comment for the table
  • createTable: Creates a table from a DDL object

The magelicious_core_log table here is essentially storage behind our MageliciousCoreModelLog simple model. If our model was an EAV model, we would be using the same InstallSchema script to create tables such as the following:

  • log_entity
  • log_entity_datetime
  • log_entity_decimal
  • log_entity_int
  • log_entity_text
  • log_entity_varchar

However, in the case of the EAV model, the actual attributes severity_level and note would then likely be added via an InstallData script. This is because attributes definitions are essentially data under the eav_attribute_* tables—primarily the eav_attribute table. Therefore, attributes are created inside of the InstallData and UpgradeData scripts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.247.31