Magento 2 Data Scripts

Magento 2 data scripts are responsible for adding data to the database. In terms of adding a module, the setup scripts will construct the table(s), and then the data scripts will import the data.

Magento 1 used this concept and added any data scripts to the ‘data’ directory in the module’s folder structure.

Within Magento 2, setup scripts are all contained within a ‘Setup’ directory. This article will be focusing on adding InstallData and UpgradeData classes.

If you’ve followed the Install Setup Scripts article, or you have added your own custom table to the database, you’ll be ready to add some data.

To this, create an InstallData.php file within your module’s ‘Setup’ directory.

// app/code/Siphor/Names/Setup/InstallData.php
<?php
namespace Siphor\Names\Setup;

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

class InstallData implements InstallDataInterface {

    protected $namesFactory;

    public function __construct(
        \Siphor\Names\Model\NamesFactory $namesFactory
    ) {
        $this->namesFactory = $namesFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    {
        $data = [
            [
                'name' => 'John Doe',
                'description' => 'This is John Doe.'
            ],
            [
                'name' => 'Jane Doe',
                'description' => 'This is Jane Doe.'
            ]
        ];

        $names = $this->namesFactory->create();

        foreach ($data as $name) {
            $names->setData($name)->save();
        }
    }
}

Within the class’ constructor, the NamesFactory class is passed in as an argument. This class does not exist by default, but is generated by Magento within the var/generation directory.

After creating this file and running the Magento setup command:

/path/to/your/php bin/magento setup:upgrade

You should notice the data inserted within the table.

Magento 2 Data Scripts

Now for the UpgradeData.php file. Earlier versions of Magento used to name their data scripts based on the current module version and the version you were upgrading the module to. For example:

data-upgrade-1.0.0-1.2.0.php

Suggests that the module is being upgraded from version 1.0.0 to 1.2.0.

As Magento 2 does not cater for multiple data scripts, we have to add version checks within the singular data upgrade class itself.

// app/code/Siphor/Names/Setup/UpgradeData.php
<?php
namespace Siphor\Names\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeData implements UpgradeDataInterface {

    protected $namesFactory;

    public function __construct(
        \Siphor\Names\Model\NamesFactory $namesFactory
    ) {
        $this->namesFactory = $namesFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    {

        if (version_compare($context->getVersion(), '1.2.0', '<')) {
            $data = [
                [
                    'name' => 'Bob Doe',
                    'description' => 'This is Bob Doe.'
                ],
                [
                    'name' => 'Alice Doe',
                    'description' => 'This is Alice Doe.'
                ]
            ];

            $names = $this->namesFactory->create();

            foreach ($data as $name) {
                $names->setData($name)->save();
            }
        }
    }
}

In the above example, we’re adding more data to our table if the module version saved in the database is less than the configured module version value within the module.xml file.

With that said, in order for the upgrade class to run, ensure that you update the module version. In this example, ‘1.0.0’ needs to be changed to ‘1.2.0’.

// app/code/Siphor/Names/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Siphor_Names" setup_version="1.2.0" />
</config>

If you run the upgrade command once more, you should notice the extra names added to the database table!

Magento 2 Data Scripts

Note: This article is based on Magento CE version 2.1.