3

I have created a new table and added a few data using Data patch setup/patch/data/.

Now I want to add some more data and want to update few data's so can anyone please help me or guide me that how can I do that?

Thanks

2

Inject Magento\Framework\Setup\ModuleDataSetupInterface on your data patch class:

Add below code add for adding to that table

        $this->moduleDataSetup->startSetup();
        $setup = $this->moduleDataSetup;

        $data[] = ['field1' => 'Value11', 'field2' => 'Value12'];
        $data[] = ['field1' => 'Value21', 'field2' => 'Value22'];

         $this->moduleDataSetup->getConnection()->insertArray(
            $this->moduleDataSetup->getTable('your_table'),
            ['field1', 'field2'],
            $data
        );     
        $this->moduleDataSetup->endSetup();

Full Data patch Class:

  • File location: Devbera\TestPracticsPlugin\Setup\Patch\Data\TestInfo .A nd Changes it according to your module and patch class.
<?php


namespace Devbera\TestPracticsPlugin\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class TestInfo implements DataPatchInterface
{



    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    public function __construct(
       ModuleDataSetupInterface $moduleDataSetup

     ) {

        $this->moduleDataSetup = $moduleDataSetup;

    }
    public function apply()
    {
        $this->moduleDataSetup->startSetup();
        $setup = $this->moduleDataSetup;

        $data[] = ['field1' => 'Value11', 'field2' => 'Value12'];
        $data[] = ['field1' => 'Value21', 'field2' => 'Value22'];

         $this->moduleDataSetup->getConnection()->insertArray(
            $this->moduleDataSetup->getTable('your_table'),
            ['field1', 'field2'],
            $data
        );     
        $this->moduleDataSetup->endSetup();
    }
    public function getAliases()
    {
        return [];
    }
    public static function getDependencies()
    {
        return [];
    }
}

1
  • Its not working, I hope you understood my question. I have already added data once using moduleDataSetup with Data patch. Now i want to add some more data and want to edit few data. Nov 29 '19 at 4:39
0

The patch will only be applied once and that is when you run bin/magento setup:upgrade

When the patch is successfully executed, Magento will insert a record into the patch_list table in the database, so in order to add some more data and edit few data, you have to remove the record from the patch_list table, which will allow the patch to be executed again when running bin/magento setup:upgrade hope the answer will help future developers

the source this is a great article where it's explained in detail.

0

You can "add some more data" as @Amit Bera suggested you and then you can "update few data" changing $this->moduleDataSetup->getConnection()->insertArray($table, array $columns, array $data); with $this->moduleDataSetup->getConnection()->update($table, array $bind, $where = '');

See this class: vendor/magento/framework/DB/Adapter/AdapterInterface.php

0

I think you need to create one more patch file for add some more data and update data's and your second file name like "FirstFileNameV2".

Then run bin/magento setup:upgrade command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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