Skip to main content
added 74 characters in body
Source Link
bchatard
  • 468
  • 3
  • 6

You can also use \Magento\Config\Model\Config::save. Below a simple sample:

$configData = [
    'section' => 'MY_SECTION',
    'website' => null,
    'store'   => null,
    'groups'  => [
        'MY_GROUP' => [
            'fields' => [
                'MY_FIELD' => [
                    'value' => $myValue,
                ],
            ],
        ],
    ],
];

// $this->configFactory --> \Magento\Config\Model\Config\Factory
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->configFactory->create(['data' => $configData]);
$configModel->save();

This syntax is not "simple", but it's more safe for some case. Du to the save logic, the action might be slower than direct access to the db.

In my case, $value need to be encrypted. In system.xml, I set the backend model for the field, and the save logic encrypt the data.

Edit: \Magento\Config\Model\Config::setDataByPath more simple to use

You can also use \Magento\Config\Model\Config::save. Below a simple sample:

$configData = [
    'section' => 'MY_SECTION',
    'website' => null,
    'store'   => null,
    'groups'  => [
        'MY_GROUP' => [
            'fields' => [
                'MY_FIELD' => [
                    'value' => $myValue,
                ],
            ],
        ],
    ],
];

// $this->configFactory --> \Magento\Config\Model\Config\Factory
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->configFactory->create(['data' => $configData]);
$configModel->save();

This syntax is not "simple", but it's more safe for some case. Du to the save logic, the action might be slower than direct access to the db.

In my case, $value need to be encrypted. In system.xml, I set the backend model for the field, and the save logic encrypt the data.

You can also use \Magento\Config\Model\Config::save. Below a simple sample:

$configData = [
    'section' => 'MY_SECTION',
    'website' => null,
    'store'   => null,
    'groups'  => [
        'MY_GROUP' => [
            'fields' => [
                'MY_FIELD' => [
                    'value' => $myValue,
                ],
            ],
        ],
    ],
];

// $this->configFactory --> \Magento\Config\Model\Config\Factory
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->configFactory->create(['data' => $configData]);
$configModel->save();

This syntax is not "simple", but it's more safe for some case. Du to the save logic, the action might be slower than direct access to the db.

In my case, $value need to be encrypted. In system.xml, I set the backend model for the field, and the save logic encrypt the data.

Edit: \Magento\Config\Model\Config::setDataByPath more simple to use

Source Link
bchatard
  • 468
  • 3
  • 6

You can also use \Magento\Config\Model\Config::save. Below a simple sample:

$configData = [
    'section' => 'MY_SECTION',
    'website' => null,
    'store'   => null,
    'groups'  => [
        'MY_GROUP' => [
            'fields' => [
                'MY_FIELD' => [
                    'value' => $myValue,
                ],
            ],
        ],
    ],
];

// $this->configFactory --> \Magento\Config\Model\Config\Factory
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->configFactory->create(['data' => $configData]);
$configModel->save();

This syntax is not "simple", but it's more safe for some case. Du to the save logic, the action might be slower than direct access to the db.

In my case, $value need to be encrypted. In system.xml, I set the backend model for the field, and the save logic encrypt the data.