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

6 Answers 6

3

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 [];
    }
}

2
  • 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.
    – Soni5041
    Nov 29, 2019 at 4:39
  • Add all data in same Data Patch, delete all data in that table, and then remove the entry of your DataPatch(TestInfo in this case above) from the patch_list table in Magento2 DB, then run php bin/magento setup:upgrade again. Need to be careful though...
    – MSQ
    Jan 12, 2022 at 13:57
1

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.

0
<?php

declare(strict_types=1);

namespace Moazzam\AddPatch\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Moazzam\AddPatch\Model\PetFactory;
use Moazzam\AddPatch\Model\ResourceModel\Pet as ResourceModelPet;

/**
 * Add data in manage_pet table
 *
 * Class AddData
 */
class AddData implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private ModuleDataSetupInterface $moduleDataSetup;

    /**
     * @var PetFactory
     */
    private PetFactory $petFactory;
    /**
     * @var resourceModelPet
     */
    private resourceModelPet $resourceModelPet;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param resourceModelPet $resourceModelPet
     * @param PetFactory $petFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        ResourceModelPet $resourceModelPet,
        PetFactory $petFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->petFactory=$petFactory;
        $this->resourceModelPet=$resourceModelPet;
    }

    /**
     * Apply method is used for apply the data patch
     *
     * @inheritdoc
     */
    public function apply()
    {
        $sampleData = [
            [
                'pet_name' => 'German Shepard',
                'customer_id' => '1'
            ],
            [
                'pet_name' => 'German Dog',
                'customer_id' => '2'
            ]
        ];
        foreach ($sampleData as $data) {
            $insertData = $this->petFactory->create()->setData($data);
            $this->resourceModelPet->save($insertData);
        }
    }

    /**
     * @inheritdoc
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * @inheritdoc
     */
    public function getAliases(): array
    {
        return [];
    }

    /**
     * @return string
     */
    public static function getVersion(): string
    {
        return " ";
    }
}
0

Each patch can only be executed once 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 with the patch_name is the full name path of our patch class.

There are two ways to modify data applied by data patch.

First, clear all data in the table you want to modify.

Then you can either:

  • Delete the record in patch_list table which has same name as the data patch class. Modify data in data patch class. Rerun bin/magento setup:upgrade

Or

  • Modify data in data patch class then rename the class, then run bin/magento setup:upgrade.

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.