Domanda

I am working on creating an Magento REST API which passes values to dynamic rows of Stores->config. Dynamic rows were already created through frontend & backend Models.

I just need to insert values through API

Here is a snippet of code I tried

 $values=[
            'service'=>'test label',
            'token'=>'1234567890',
            'value'=>1
        ];
        $this->_configWriter->save('integration/general/tokens',$values,ScopeConfigInterface::SCOPE_TYPE_DEFAULT,0);

But the values are not getting populated. Any help would be appreciated enter image description here

È stato utile?

Soluzione

You have to change your value array and then you have to pass serialize data in database. try this code.

$id = "_" . time() . "_" . date("s");
$values = [
    $id => [
        'service' => 'test label',
        'token' => '1234567890',
        'value' => 1,
    ],
];

and then serialize this value using Magento\Framework\Serialize\Serializer\Json class serialize($value) method as below

 $values = $this->serializer->serialize($values);

and then store it to database using.

$this->_configWriter->save('integration/general/tokens',$values,ScopeConfigInterface::SCOPE_TYPE_DEFAULT,0);

I hope this will help you.

Altri suggerimenti

I've had this issue a few times. Dynamic Rows data stored in the database looks like this:

Single Row

[{"record_id":0,"service":"test label","token":"1234567890","value":"1","initialize":"true"}]

And this for multiple rows

[{"record_id":0,"service":"test label","token":"1234567890","value":"1","initialize":"true"},{"record_id":1,"service":"test label 2","token":"0987654321","value":"1","initialize":"true"}]

For new records you could do something like

 $values=[
        'record_id'=>0,
        'service'=>'test label',
        'token'=>'1234567890',
        'value'=>1,
        'initialize'=>'true'
    ];

Then add that to existing records with

array_push($records, $values); 
json_encode(records)
$this->_configWriter->save('integration/general/tokens',$records,ScopeConfigInterface::SCOPE_TYPE_DEFAULT,0);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top