My questions:

  1. I want know how this works by each and every line of code
  2. I want to add new attribute here
  3. Difference between InstallData and InstallSchema

Here is InstallData.php

namespace vendor\Module\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
    private $eavSetupFactory;

    private $eavConfig;

    private $attributeResource;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->attributeResource = $attributeResource;

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->removeAttribute(Customer::ENTITY, "skype");

        $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
        $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

        $eavSetup->addAttribute(Customer::ENTITY, 'skype', [
            // Attribute parameters
            'type' => 'varchar',
            'label' => 'Skype Account',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 990,
            'position' => 990,
            'system' => 0,

        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'skype');
        $attribute->setData('attribute_set_id', $attributeSetId);
        $attribute->setData('attribute_group_id', $attributeGroupId);

        //You can use this attribute in the following forms

        $attribute->setData('used_in_forms', [


1 Answer 1


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.

  • update the data in table ????? Commented Mar 11, 2019 at 7:20
  • for update data you need to create upgradeschema Commented Mar 11, 2019 at 7:23
  • can you explain i added code line by line Commented Mar 11, 2019 at 7:23

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.