PHPackages                             bdc/module-declarative - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. bdc/module-declarative

ActiveMagento2-module[Framework](/categories/framework)

bdc/module-declarative
======================

BDCrops Declarative module for Magento 2 extensions.

702PHP

Since Nov 6Pushed 6y ago3 watchersCompare

[ Source](https://github.com/bdcrops/BDC_Declarative)[ Packagist](https://packagist.org/packages/bdc/module-declarative)[ RSS](/packages/bdc-module-declarative/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

BDC\_Declarative Schema Module Declarative Schema Tutorial
----------------------------------------------------------

[](#bdc_declarative-schema-module-declarative-schema-tutorial)

Declarative Schema: Magento 2.3x introduced a new declarative schema feature which aims at eliminating the excessive work and speeding up the installation and upgrade processes.

Most module developers will be familiar with creating php scripts for creating, updating and manipulating custom tables for their modules. These files would live in the module directory here;

- /app/code/NameSpace/ModuleName/Setup/InstallSchema.php
- /app/code/NameSpace/ModuleName/Setup/UpgradeSchema.php

Since Magento 2.3.0, you can now use the declarative DB schema XML to create and update your DB tables in a single XML file. This file is placed in your module here;

/app/code/NameSpace/ModuleName/etc/db\_schema.xml

This change is incredibly useful once you start playing with it. The php schema scripts can be long winded and difficult to read. The XML is short and neat, making it a relative dream to work with.

Goal:
-----

[](#goal)

- Create table (etc/db\_schema.xml)
- Schema whitelist (etc/db\_schema\_whitelist.json)
- DataPatch / Insert data (Installing and upgrading data)
- How to install &amp; upgrade BDC\_Declarative
- FAQ of Declarative Schema
- Certified Associate/Professional Developer Questions &amp; Solution

1. How to install &amp; upgrade Declarative Schema Module
---------------------------------------------------------

[](#1-how-to-install--upgrade--declarative-schema-module)

### 1.1. Copy and paste

[](#11-copy-and-paste)

If you don't want to install via composer, you can use this way.

- Download [the latest version here](https://github.com/bdcrops/module-declarative/archive/master.zip)

### 1.2. Install via composer

[](#12-install-via-composer)

```
composer require bdc/module-declarative

```

---

2. Declarative Schema Module Step By Step
-----------------------------------------

[](#2-declarative-schema-module-step-by-step)

- Create [app/code/BDC/Declarative/registration.php](registration.php)

    Source```

     ```

    ```
- Create [etc/db\_schema.xml](etc/db_schema.xml)

     Source```
     ```

     ```

    ```

     file “db\_schema.xml” inside folder “BDC/Declarative/etc” and write the following code

     Show Image[![db_schema](https://github.com/bdcrops/BDC_Declarative/raw/master/view/adminhtml/web/images/db_schema.png)](https://github.com/bdcrops/BDC_Declarative/blob/master/view/adminhtml/web/images/db_schema.png)
- Create [Setup/Schema/Sample.php](Setup/Schema/Sample.php)

     Source```
     ```

--
--This below content oly if exist declaration

--

```

Then run following command to generate db\_schema\_whitelist.json

```
php bin/magento setup:db-declaration:generate-whitelist --module-name=BDC_Declarative
php bin/magento setup:upgrade --dry-run=1 --keep-generated
php bin/magento setup:upgrade

```

[![](docs/b4addcolumn.png)](docs/b4addcolumn.png)[![](docs/dryRun.png)](docs/dryRun.png)[![](docs/afterAddcolumn.png)](docs/afterAddcolumn.png)

You can modify code according to your requirement.

### How do you delete a column?

The following example removes the date\_closed column by deleting its column node. To drop a column declared in another module, redeclare it with the disabled attribute set to true.

```

-

```

It is possible to drop a column only if it exists in the db\_schema\_whitelist.json file.

### "Declarative Schema" vs "Extension Attributes" ?

"Declarative Schema" is used when you need to create a NEW table inside Magento. With declarative schema you have the advantages of mutations. "Extension Attributes" are used to add new fields inside an EXISTING table. In this way you don't extend the original model.So in the above example, the best approach is to use Extension Attributes. magento-2-what-are-extension-attributes

### How do you add an index ?

Add an index: The following example adds the INDEX\_SEVERITY index to the declarative\_table table.

```

+
+
+

```

### How do You add foreign key using declarative schema?

- Create a foreign key In the following example, the selected constraint node defines the characteristics of the FL\_ALLOWED\_SEVERITIES foreign key.

```

+

```

- Drop a foreign key The following example removes the FL\_ALLOWED\_SEVERITIES foreign key by deleting its constraint node. To drop a constraint declared in another module, redeclare it with the disabled attribute set to true.

```

-

```

It is possible to drop a foreign key only if it exists in the db\_schema\_whitelist.json file.

- Recreate a foreign key In this example, Module A defines a new table with primary key id\_column. Module B declares its own schema, in which it creates a new column (new\_id\_column) and changes the primary index to this column. Module B disables the original primary key and sets a new primary key with a referenceId value that is different from PRIMARY. Although this value is different, the real name of the primary key in the database remains PRIMARY.

Module A declaration

```

```

Module B declaration

```

```

### How do you manipulate data using data patches?

A data patch is a class that contains data modification instructions. It is defined in a /&lt;Module\_Name&gt;/Setup/Patch/Data/&lt;Patch\_Name&gt;.php file and implements \\Magento\\Framework\\Setup\\Patch\\DataPatchInterface.

### What is the purpose of schema patches?

A schema patch contains custom schema modification instructions. These modifications can be complex. It is defined in a /&lt;Module\_Name&gt;/Setup/Patch/Schema/&lt;Patch\_Name&gt;.php file and implements \\Magento\\Framework\\Setup\\Patch\\SchemaPatchInterface.

Unlike the declarative schema approach, patches will only be applied once. A list of applied patches is stored in the patch\_list database table. An unapplied patch will be applied when running the setup:upgrade from the Magento CLI.

Magento 2 Certified Professional Developer exam
-----------------------------------------------

### 4.4 Demonstrate an ability to use declarative schema

- How do you add a column using declarative schema?
- How do you modify a table added by another module? ||
- How do you delete a column? ||
- How do you add an index or foreign key using declarative schema? ||
- How do you manipulate data using data patches?
- What is the purpose of schema patches?

### How to manipulate columns and keys using declarative schema?

- Add a column to table The following example adds the date\_closed column. [![](https://camo.githubusercontent.com/ad8993483d9c298ec636eed66d51e80c173af87e44a49e63319485f377dc6086/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6164642d636f6c756d6e2e706e67)](https://camo.githubusercontent.com/ad8993483d9c298ec636eed66d51e80c173af87e44a49e63319485f377dc6086/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6164642d636f6c756d6e2e706e67)
- Drop a column from a table The following example removes the date\_closed column by deleting its column node. To drop a column declared in another module, redeclare it with the disabled attribute set to true. [![](https://camo.githubusercontent.com/1cf507b37160d3b0f1f939c4fee617d5f369bc0e7bc8df91eaa84e028b1797aa/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f72656d6f76652d636f6c756d6e2e706e67)](https://camo.githubusercontent.com/1cf507b37160d3b0f1f939c4fee617d5f369bc0e7bc8df91eaa84e028b1797aa/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f72656d6f76652d636f6c756d6e2e706e67)
- Change the column type The following example changes the type of the title column from varchar to tinytext. [![](https://camo.githubusercontent.com/a4803b2d9afe0bd177482eec2c0f2a396bab8ed7fdfa04dfcd511b7ade71dcf2/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6368616e67652d636f6c756d6e2d747970652e706e67)](https://camo.githubusercontent.com/a4803b2d9afe0bd177482eec2c0f2a396bab8ed7fdfa04dfcd511b7ade71dcf2/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6368616e67652d636f6c756d6e2d747970652e706e67)
- Rename a column To rename a column, delete the original column declaration and create a new one. In the new declaration, use the onCreate attribute to specify which column to migrate data from. Use the following construction to migrate data from the same table. onCreate="migrateDataFrom(entity\_id)"

To migrate data from another table, specify a value similar to the following: onCreate="migrateDataFromAnotherTable(catalog\_category\_entity,entity\_id)"

- Add an index The following example adds the INDEX\_SEVERITY index to the table\_name table.

[![](https://camo.githubusercontent.com/91e16cd68abba76da1504f5c252384db3f1efc8d21e759056674577ab04aef49/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6164642d696e6465782e706e67)](https://camo.githubusercontent.com/91e16cd68abba76da1504f5c252384db3f1efc8d21e759056674577ab04aef49/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6164642d696e6465782e706e67)

- Create a foreign key In the following example, the selected constraint node defines the characteristics of the FL\_ALLOWED\_SEVERITIES foreign key.

[![](https://camo.githubusercontent.com/a12da5664b03af3b46276416776d99de081f2b660bbd1d17729d9ef4160811b1/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6372656174652d666b2e706e67)](https://camo.githubusercontent.com/a12da5664b03af3b46276416776d99de081f2b660bbd1d17729d9ef4160811b1/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f6372656174652d666b2e706e67)

- Drop a foreign key The following example removes the FL\_ALLOWED\_SEVERITIES foreign key by deleting its constraint node. To drop a constraint declared in another module, redeclare it with the disabled attribute set to true. [![](https://camo.githubusercontent.com/839324ec1f90d39a3cde5f0be141586156abcd7fac7e51135dd5d726ed5067fe/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f64726f702d666b2e706e67)](https://camo.githubusercontent.com/839324ec1f90d39a3cde5f0be141586156abcd7fac7e51135dd5d726ed5067fe/68747470733a2f2f6d6167656e746f2d646576646f63732e6769746875622e696f2f646576646f63732d666f722d74657374732f6775696465732f76322e332f657874656e73696f6e2d6465762d67756964652f6465636c617261746976652d736368656d612f696d616765732f64726f702d666b2e706e67)

### What is the purpose of whitelisting?

Purpose of whitelisting: You will not be able to run a declarative mode without creating a schema whitelist. Since backward compatibility must be maintained, declarative schema doesn’t automatically delete database tables, columns or keys not defined in db\_schema.xml.This is one of the reasons we have db\_schema\_whitelist.json. It shows a history of all tables, columns and keys added with declarative schema and it’s required for drop operations.

Note: it is recommended to generate a new whitelist for every release for the double-check purposes. Before running the upgrade command you need to add your schema to db\_whitelist\_schema.json file by running the following command. For that, you need a //etc/db\_schema\_whitelist.json file that will store all the content added with declarative schema. To generate this file, run:

[![db_schema](https://github.com/bdcrops/BDC_Declarative/raw/master/view/adminhtml/web/images/whitelist.png)](https://github.com/bdcrops/BDC_Declarative/blob/master/view/adminhtml/web/images/whitelist.png)

```
php bin/magento setup:db-declaration:generate-whitelist [options]
php bin/magento setup:db-declaration:generate-whitelist --module-name=vendor_module
php bin/magento setup:db-declaration:generate-whitelist --module-name=BDC_Declarative
php bin/magento setup:upgrade --dry-run=1 --keep-generated

```

Now, there are db\_whitelist\_schema.json file will be create in /vendor/module/etc folder.

There are options you can add at the end of that command. For instance, you can use “–module-name=YourModule” to specify the module you want to generate a whitelist for. Similarly, you could also set “–module-name=all” although it will generate a whitelist for all modules by default.

### How to use Data and Schema patches?

- Data Patches: [![](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-23-638.jpg)](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-23-638.jpg)[![](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-25-638.jpg)](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-25-638.jpg)
- Schema Patches : [![](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-6-638.jpg)](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-6-638.jpg)

### How to manage dependencies between patch files?

schema patch contains custom schema modification instructions. These modifications can be complex. It is defined in a /&lt;Module\_Name&gt;/Setup/Patch/Schema/&lt;Patch\_Name&gt;.php file and implements \\Magento\\Framework\\Setup\\Patch\\SchemaPatchInterface.

Unlike the declarative schema approach, patches will only be applied once. A list of applied patches is stored in the patch\_list database table. An unapplied patch will be applied when running the setup:upgrade from the Magento CLI.

Optionally, if you plan to enable rollback for your patch during module uninstallation, then you must implement \\Magento\\Framework\\Setup\\Patch\\PatchRevertableInterface.

Old scripts will work with new versions of Magento. However, if you want to convert your old scripts to the new format, implement \\Magento\\Framework\\Setup\\Patch\\PatchVersionInterface. This interface allows you to specify the setup version of the module in your database. If the version of the module is higher than or equal to the version specified in your patch, then the patch is skipped. If the version in the database is lower, then the patch installs

### How to EAV attributes Create via Declarative Schema ?

In Magento 2.3 and above, you can create attribute via DataPatch. For example, create a file: Vendor\_Module\\Setup\\Patch\\Data\\ApplyNewAttribute.php

```
