11

I want to know the difference between InstallSchema and InstallData in details.

I also need to know when can I use InstallSchema.php and InstallData.php?

1
  • Let me know if you still have any issue , otherwise accept answer so other user gets helped Commented Aug 2, 2017 at 9:36

4 Answers 4

21

First both InstallSchema And InstallData both method are called when you install any new module

Schema setup scripts change database schema, they create or change needed database tables. If module is installing, Setup\InstallSchema::install() is executed.

It means it create new table and its fields with its structure.

InstallData setup scripts contain entries module needs to insert into database. Attributes that come with Magento by default,Cms pages, various default groups and roles, are all examples of data setup.

Data setup is executed after Schema setup, they function in a similar fashion.

So come to the conclustion, Main difference between both is InstallSchema is used to make table and its structure. While InstallData is used to insert/add data into table.

2
  • Means,InstallData use for add attribute for Customer,Qoute,Order,Catalog,Product,Category. Commented Jun 15, 2017 at 7:15
  • Well you partially correct , InstallData is used to InsertData of attribute (value of attribute) Lets say you have created table called "Slider" by installSchema Now you need to insert each slide Data then you need to use InstallData file and define data of each slide . Make sense ? Commented Jun 15, 2017 at 7:54
9

Magento2 uses Four Type of files for the Initial operations related to database that are executed just with the installation process of module (These files runs only once through out of their existence). Those files are as follows

  1. InstallSchema.php
  2. InstallData.php
  3. UpgradeSchema.php
  4. UpgradeData.php

All of these files are present ate Magento_root/{app/Vendor}/{Vendor_Name}/{Module_Name}/Setup Name space of your magento module. Here I have described all the files with their use.

InstallSchema.php

This file is executed first just after your modules registration (Means just after your module & its version entries are done in to the table -> setup_module). This file is used to create tables with their columns attribute into your database that are later used by the new installed module.

InstallData.php

This file is executed after InstallSchema.php. It is used to add data to the newly created table or any existing table.

UpgradeSchema.php

This file comes with the module & runs only then, if you are already having that modules previous version installed in your magento(Means it has entry of its previous version into the table -> setup_module). It is used to manipulate the table related to the module(Means it is used to alter the table schema means columns attribute & to add new column into that table).

UpgradeData.php

This file runs after UpgradeSchema.php. It is having the same concept as InstallData.php has but using this file you can change/alter the database contents without the use of model files. You can also use this file to add new content to the database same us InstallData.php. But same like UpgradeSchema.php it will also runs only then if you are having that modules previous version installed in your magento.

As I said in starting these files runs only once so the basic use of this files is to prepare the tables & data for the Module which is been get used by the module through out its existence.

Note: Magento uses these files to create tables that are used into Magentos functionalities & to add the contents like cms_contents, customer_groups & attributes, Products_Types & attributes, Taxation groups etc.

8

There are following difference,

InstallSchema

InstallSchema.php purpose is to create new table/attribute/fields in Database

InstallData

InstallData.php purpose is to insert Data/value into database.

4

There is basic difference between 2 is

InstallSchema will run when the module is installed to setup the database structure

While InstallData will run when the module is installed to initial the data for database table

Hoope this clear your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.