In this article, we are going to explain all about declarative schema. Firstly, we would like to talk about:
What is a DB schema?
Magento 2.3 comes with its new feature declarative schema, which plays an important role in creating, adding, and updating data in the database table. In the previous version of Magento developers needed to write various scripts such as InstallData or InstallSchema and UpgradeSchema.php to create a new table and add/update new data in the database table. But the Db Schema is one of the major changes in Magento 2.3. It is a very effective and easy way to work with a database. Here you will know why and how to use a declarative schema in Magento 2.
Why do we use declarative schema?
- Installing and upgrading a code can be handled in a single XML file.
- Boost Performance
- Time-Saving
By using a declarative schema approach developers don’t need to write scripts for the latest version of any module. It allows data to be deleted when an extension is uninstalled.
How to use declarative schema?
A DB scheme comes in the form of an XML file (in the XML Format). You can create this by adding a db_schema.xml file inside /Webiators/TableCreateByDbSchema/etc
and write the following code:
Here “Webiators” is the Vendor and “TableCreateByDbSchema” is the Module name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0"?> <!-- /** * @category Webiators * @package Webiators_TableCreateByDbSchema * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ --> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="webiators_dbschema" resource="default" engine="innodb" comment="Webiators DB Schema"> <column xsi:type="int" name="employee_id" unsigned="true" nullable="false" identity="true" comment="Employee ID"/> <column xsi:type="text" name="employee_name" comment="Employee Name"/> <column xsi:type="text" name="employee_department" comment="Employee Department"/> <column xsi:type="text" name="mobile_no" comment="Mobile Number"/> <column xsi:type="smallint" name="status" nullable="false" comment="Status"/> <column xsi:type="text" name="logo" nullable="false" comment="Image"/> <constraint xsi:type="primary" referenceId="PRIMARY"> <column name="employee_id"/> </constraint> </table> </schema> |
<table> .. </table>
= “Use for creating and set table name”<column> .. </column>
= “Use for creating and set a column of the table”<constraint> .. </constraint>
= “Use for set constraint as like primary key, foreign key, unique key etc.”
Now you need to add your schema to db_whitelist_schema.json
file by running the following command Before running the upgrade command.
1 |
php bin/magento setup:db-declaration:generate-whitelist --module-name=Webiators_TableCreateByDbSchema |
Now, you will see that db_whitelist_schema.json
file will be created in /Webiators/TableCreateByDbSchema/etc
folder.
Now you need to run the following command given bellow:
1 2 3 4 5 6 |
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f en_US php bin/magento cache:flush php bin/magento cache:clean php bin/magento indexer:reindex |
Let’s check your database and see the result
We hope this blog post would be very helpful for you… 🙂
Thank You!
Read More