@@ -241,61 +241,32 @@ template.
241
241
242
242
## Database Setup
243
243
244
- Magento 2 uses database setup scripts to create and modify database tables and perform other setup tasks. To create a
245
- database setup script, follow these steps:
244
+ Magento 2 uses Declarative schema setup to create and modify database tables and perform other setup tasks. To create a
245
+ declarative schema script, follow these steps:
246
246
247
- 1 . Create a file named ` InstallSchema.php ` inside your module's ` Setup ` directory.
248
- 2 . Implement the necessary logic inside the class. Here's an example:
249
-
250
- ``` php
251
- <?php
252
- namespace Vendor\Module\Setup;
247
+ 1 . Create a file named ` db_schema.xml ` inside your module's ` etc ` directory.
248
+ 2 . Implement the necessary logic inside the file. Here's an example:
253
249
254
- use Magento\Framework\Setup\InstallSchemaInterface;
255
- use Magento\Framework\Setup\ModuleContextInterface;
256
- use Magento\Framework\Setup\SchemaSetupInterface;
257
-
258
- class InstallSchema implements InstallSchemaInterface
259
- {
260
- public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
261
- {
262
- $setup->startSetup();
263
-
264
- $table = $setup->getConnection()
265
- ->newTable($setup->getTable('custom_table'))
266
- ->addColumn(
267
- 'entity_id',
268
- \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
269
- null,
270
- ['identity' => true, 'nullable' => false, 'primary' => true],
271
- 'Entity ID'
272
- )
273
- ->addColumn(
274
- 'name',
275
- \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
276
- 255,
277
- ['nullable' => false],
278
- 'Name'
279
- )
280
- ->addColumn(
281
- 'created_at',
282
- \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
283
- null,
284
- ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
285
- 'Created At'
286
- )
287
- ->setComment('Custom Table');
288
-
289
- $setup->getConnection()->createTable($table);
290
-
291
- $setup->endSetup();
292
- }
293
- }
250
+ ``` xml
251
+ <table name =" custom_table" resource =" default" engine =" innodb"
252
+ comment =" Custom Table" >
253
+ <column xsi : type =" int" name =" entity_id" unsigned =" false" nullable =" false" identity =" true" comment =" Entity ID" />
254
+ <column xsi : type =" varchar" name =" name" nullable =" false" length =" 255" default =" " comment =" name" />
255
+ <column xsi : type =" timestamp" name =" created_at" on_update =" false" nullable =" false" default =" CURRENT_TIMESTAMP" comment =" Created At" />
256
+ <constraint xsi : type =" primary" referenceId =" PRIMARY" >
257
+ <column name =" entity_id" />
258
+ </constraint >
259
+ </table >
294
260
```
295
261
296
262
In this example, we're creating a table named ` custom_table ` with three columns: ` entity_id ` , ` name ` , and ` created_at ` .
297
263
298
- 3 . Run the setup upgrade command to apply your schema changes:
264
+ 3 . Run the whitelist generation command:
265
+
266
+ ```
267
+ bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Module
268
+ ```
269
+ 4 . Run the setup upgrade command to apply your schema changes:
299
270
300
271
``` shell
301
272
bin/magento setup:upgrade
0 commit comments