cancel
Showing results for 
Search instead for 
Did you mean: 

How to store variables for later use?

How to store variables for later use?

I have two variables, let's call them X and Y. I want to access these two variables and change them as I wish from different parts of my extension. I wanted to use the $this->scopeConfig to store my values, but it looks like I can only get variables, not set them. Any suggestions? I don't want to create a database for this task.

1 REPLY 1

Re: How to store variables for later use?

Method 1 :
You can store variables in already existing table in magento 
table_name = flag
You can search this in the magento database tables. In this you can store the variables and its value and then later get them. (Its safe to use the magento flag table)
In this way you can do the same.

public function __construct(
        \Magento\Framework\FlagManager $flagManager
    ) {
        $this->flagManager = $flagManager;
    }

public function setConfigFlagValue($flagCode, $value) {
        $this->flagManager->saveFlag($flagCode, $value);
        return true;
    }

public function getConfigFlagValue($flagCode) {
        $flagValue = $this->flagManager->getFlagData($flagCode);
        return $flagValue;
    }
// Desired variable name
$flagCode = 'variable_name';

// Desired variable value
$value = 'variable_value';

You can use this function anywhere by putting the above code in common Helper file and then call the functions :
                setConfigFlagValue($flagCode, $value) to set/update the value
                getConfigFlagValue($flagCode) to get the value
when its required.

Method 2 :
You can use the Magento sessions to get the value and set them . But it will not persist for long time. For short time storing you can use them.

Click on 'Kudos' & Accept as Solution to encourage to write more answers !