0

On an admin grid form I have a checkbox. If the checkbox is checked the form shows additional data fields. If it is not checked the additional fields are hidden. When the form is saved, the data from the fields are saved to the db.

Here is my previous question with this behavior as outcome: Dynamic Fields in Admin Grid

First I check the checkbox, add data to the additional data fields and save the form. At a later point I decide (however), that this entity does not need the additional data. I uncheck the checkbox and save the form. The problem now is, that the additional data is still in the db. But the additional data should be removed from the db. So my goal is to overwrite the existing data with the NULL value when the checkbox is not checked.

How can I achieve that?

1
  • please add your previous question's link. so its easy to understand for someone. – Gohil Rajesh May 12 at 15:53
0

You use a patch to achieve that. the patch will run automatically when you run setup:upgrade.

Create a file in your module in Setup\Patch\Data

<?php

namespace Your\Module\Setup\Patch\Data;

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

/**
 * Class UpdateCustomValue
 * @package Your\Module\Setup\Patch\Data
 */
class UpdateCustomValue implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var \Magento\Directory\Helper\DataFactory
     */
    private $directoryDataFactory;

    /**
     * InitializeDirectoryData constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param \Magento\Directory\Helper\DataFactory $directoryDataFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        \Magento\Directory\Helper\DataFactory $directoryDataFactory
    ) {
        $this->moduleDataSetup      = $moduleDataSetup;
        $this->directoryDataFactory = $directoryDataFactory;
    }

    /**
     * @inheritdoc
     *
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function apply()
    {
        // Here replace your table name and set the
        $table = $this->moduleDataSetup->getTable('your_table_name');
        $this->moduleDataSetup->getConnection()->update(
            $table,
            ['your_column_name' => 'null'],
            ['entity_id= ?' => 1]
        );
    }

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

    /**
     * @inheritdoc
     */
    public static function getVersion()
    {
        return '2.0.0';
    }

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

Note: if you want to rerun the patch in your local development environment you need to delete your patch entry from patch_list table.

2
  • Thank you for your answer, but I think, this is not what I want. The Patch is something that you do to get data in your database. In my case the data is already there and can be edited in an admin grid. So I need a solution to overwrite the existing data with null values, when the admin grid form is saved by a Backend user. – Frank Mohaupt May 12 at 15:11
  • You can run insert, Update queries also, in my answer i have used update which will set values to null. – Rizwan Khan May 17 at 6:53

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.