Hello Magento Folks,
As you know from magento 2.3, use db_schema.xml for creating a new table for the database instead of written InstallSchema PHP class. The new database table creation/updating concept in Magento 2.3 is called declarative schema.
As you know from magento 2.3, use db_schema.xml for creating a new table for the database instead of written InstallSchema PHP class. The new database table creation/updating concept in Magento 2.3 is called declarative schema.
Also From Magento 2.3 We don’t need to create InstallSchema.php and UpgradeSchema.php
Now you can create a database table using a new way by creating DB schema XML file lets take a look.
Create a file db_schema.xml
path
app/code/{Vendor}/{Module}/etc/db_schema.xml
path
app/code/{Vendor}/{Module}/etc/db_schema.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="test_table" resource="default" engine="innodb" comment="My Test Table">
<column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true"
comment="Entity Id"/>
<column xsi:type="varchar" name="username" nullable="true" length="50" comment="User name"/>
<column xsi:type="varchar" name="email" nullable="true" length="255" comment="Email"/>
<column xsi:type="text" name="bio" nullable="true" comment="bio"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
</constraint>
</table>
</schema>
|
We have taken different field types for creating the new database table. You can take the only required onc.
<table>…</table> : “Used for create table in Magento 2.3+”
<column>…</column> : “Used for create different column of the table”
You can set int, text, varchar, timestamp, date, datetime, smallint, float, decimal, double etc.
<constraint>…</constraint> : “Used for set of constraint, Like primary key, foreign key, unique key.”
<column>…</column> : “Used for create different column of the table”
You can set int, text, varchar, timestamp, date, datetime, smallint, float, decimal, double etc.
<constraint>…</constraint> : “Used for set of constraint, Like primary key, foreign key, unique key.”
Upgrade your database to create/update table in Magento 2.3 by using below command
1
|
php bin/magento setup:upgrade
|
Now you can check in database new table with name “test_table” with all added column will be created.
Hope this code helped you. please feel free to comment in case of any issue.
Happy Coding
Happy Coding
No comments:
New comments are not allowed.