Table of Contents for
Magento 2 Development Quick Start Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Magento 2 Development Quick Start Guide by Branko Ajzele Published by Packt Publishing, 2018
  1. Magento 2 Development Quick Start Guide
  2. Title Page
  3. Copyright and Credits
  4. Magento 2 Development Quick Start Guide
  5. Packt Upsell
  6. Why subscribe?
  7. Packt.com
  8. Contributors
  9. About the author
  10. About the reviewer
  11. Packt is searching for authors like you
  12. Table of Contents
  13. Preface
  14. Who this book is for
  15. What this book covers
  16. To get the most out of this book
  17. Download the example code files
  18. Code in Action
  19. Conventions used
  20. Get in touch
  21. Reviews
  22. Understanding the Magento Architecture
  23. Technical requirements
  24. Installing Magento
  25. Modes
  26. Areas
  27. Request flow processing
  28. Modules
  29. Creating the minimal module
  30. Cache
  31. Dependency injection
  32. Argument injection
  33. Virtual types
  34. Proxies
  35. Factories
  36. Plugins
  37. The before plugin
  38. The around plugin
  39. The after plugin
  40. Events and observers
  41. Console commands
  42. Cron jobs
  43. Summary
  44. Working with Entities
  45. Technical requirements
  46. Understanding types of models
  47. Creating a simple model
  48. Methods worth memorizing
  49. Working with setup scripts
  50. The InstallSchema script
  51. The UpgradeSchema script
  52. The Recurring script
  53. The InstallData script
  54. The UpgradeData script
  55. The RecurringData script
  56. Extending entities
  57. Creating extension attributes
  58. Summary
  59. Understanding Web APIs
  60. Technical requirements
  61. Types of users
  62. Types of authentication
  63. Types of APIs
  64. Using existing web APIs
  65. Creating custom web APIs
  66. Understanding search criteria
  67. Summary
  68. Building and Distributing Extensions
  69. Technical requirements
  70. Building a shipping extension
  71. Distributing via GitHub
  72. Distributing via Packagist
  73. Summary
  74. Developing for Admin
  75. Technical requirements
  76. Using the listing component
  77. Using the form component
  78. Summary
  79. Developing for Storefront
  80. Technical requirements
  81. Setting up the playground
  82. Calling and initializing JS components
  83. Meet RequireJS
  84. Replacing jQuery widget components
  85. Extending jQuery widget components
  86. Creating jQuery widgets components
  87. Creating UI/KnockoutJS components
  88. Extending UI/KnockoutJS components
  89. Summary
  90. Customizing Catalog Behavior
  91. Technical requirements
  92. Creating the size guide
  93. Creating the same day delivery
  94. Flagging new products
  95. Summary
  96. Customizing Checkout Experiences
  97. Technical requirements
  98. Passing data to the checkout
  99. Adding order notes to the checkout
  100. Summary
  101. Customizing Customer Interactions
  102. Technical requirements
  103. Understanding the section mechanism
  104. Adding contact preferences to customer accounts
  105. Adding contact preferences to the checkout
  106. Summary
  107. Other Books You May Enjoy
  108. Leave a review - let other readers know what you think

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 \Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

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 Magento\Framework\DB\Adapter\Pdo\Mysql.

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',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Entity ID'
)->addColumn(
'severity_level',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
24,
['nullable' => false],
'Severity Level'
)->addColumn(
'note',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
['nullable' => false],
'Note'
)->addColumn(
'created_at',
\Magento\Framework\DB\Ddl\Table::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 Magelicious\Core\Model\Log 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.