Insert data while instaling module (InstallData) – setup script – Magento 2

Updated: Aug 20, 2020

This is a continuation of the previous tutorial, It helps to insert data into the database while installing module in Magento 2.

As per our previous tutorial, we summarize the below points

  1. InstallSchema.php script executed while running “bin/magento setup:upgrade” , we use this features to create table structure

  2. Create CRUD object using Model, ResourceModel and Collection for CRUD(Create Read Update Delete) operation

Now we use the same CRUD object to insert data into our custom table.

  1. InstallData.php script also executed while run “bin/magento setup:upgrade” , we use this features to insert data into our custom table.

Create InstallData Script

Create app/code/Vendor/Module/Setup/InstallData.php

<?php
namespace Bilal\Cruddemo\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $_cruddemoFactory;

    public function __construct(\Bilal\Cruddemo\Model\CruddemoFactory $cruddemoFactory)
    {
        $this->_cruddemoFactory = $cruddemoFactory;
    }


    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $data = [
            'name' => 'Dinesh',
            'age' => '26'
        ];

        $this->_cruddemoFactory->create()->setData($data)->save();
    }

}

As we discussed InstallData.php execute while running setup:upgrade so run the below command in your Magento root

php bin/magento setup:upgrade

If you check your table, not found our new data. What is happening?

setup:upgrade command directly check into setup_module table for the entry Bilal_Cruddemo (Vendor_Module)

  1. If it is found, it skips execution of install script

  2. If it is not found, it executes  InstallData

For the confirmation, run the below query to delete the table and remove our module entry from the setup_module table.

DROP TABLE `crud_test_emp1`
DELETE FROM `setup_module` WHERE `module`='Bilal_Cruddemo';

Now run the below commands

php bin/magento setup:upgrade

If you check your table, you can found our new data.


I hope it helps! Please carry the same source code to follow next tutorial.

#crud #installData #magento2

3 views0 comments

Recent Posts

See All

Magento 2 CMS block with the same ID already exists

I have seen the below error when I open cms block grid something went wrong with processing the default view and we have restored the filter to its original state. I found the below log in the excepti

How to generate bearer token - Magento 2 REST API

We can generate the admin bearer token using the below command. Please run the below command in the terminal to generate a token. curl -XPOST -H 'Content-Type: application/json' http://www.domain.com/

Magento 2 script tag and available options in layout xml

We can add a script via layout XML file in Magento 2. The below format will stop the HTML parse until js downloaded and executed. <script src="Bilal_Usean/js/sample.min.js" /> The below format will st

Follow Me

  • LinkedIn
  • Twitter
  • Facebook
  • YouTube
  • Instagram

©2020 by Bilal Usean. Proudly created with Wix.com