6

I have a collection of related configs that i would like to be retrieved as an array. Using store config, i can't see that this is possible.

Fore example i want the following:

array('myvals' => 
    array('key1' => 'val1'),    array('key1' => 'val2', 'key1' => 'val3'),

)

But i want this to be managed via store config and retrieved via Mage::getStoreConfig('myvals');

Is this possible in anyway?

Marty Wallace
  • 5,631
  • 13
  • 65
  • 90
  • What are you expecting? getStoreConfig will always return a string based on a config path - e.g.: Mage::getStoreConfig('web/default/cms_home_page') == 'home'. You can see these values by inspecting the core_config_data table's scope_id, path and value fields in your database. – Robin Winslow Sep 02 '13 at 12:39
  • this is precisely my question - is there any way to get an array returned from config instead :) – Marty Wallace Sep 02 '13 at 12:55

2 Answers2

11

You cannot build this type of multi-dimensional arrays in the store config. If you want to store an array in the store_config you will need to serialize them using the backend-model for the field you want to store:

<backend_model>adminhtml/system_config_backend_serialized_array</backend_model>

With this backend you can build your own frontend model that extends the Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract, here you can define your own fields you need (only text inputs can be created by default). When saving this will create an array that is stored serialized in the database, so when reading the value you will need to unserialize is:

$myvals = unserialize(Mage::getStoreConfig('myvals'));

UPDATE: If you do not need the backend input fields you can also save it yourself (and you should be able to use the multi-dimension):

Mage::getConfig()->saveConfig('myvals',serialize($myvals));
Vladimir Kerkhoff
  • 8,223
  • 1
  • 35
  • 41
0

There is no standard way in Magento to do this. You'll have to write custom code. You can use custom queries on the core_config_data table. For example a custom module with a method getConfig() that runs this query:

SELECT * FROM core_config_data WHERE path LIKE 'myvals/%'

And then a toArray() method that splits the result up using explode('/', $row) so that you could use code like this:

$vals = Mage::getModel('custom_config')->getConfig('myvals');
$val3 = $vals['key1'];

But logically this doesn't make a lot of sense. It's better to have a good design with a distinct number of configurable items. If you're adding new config items, you'll be customizing your code anyway.

By the way what is your use case? Is there a reason besides prettier code that you need this?

Jacques
  • 1,026
  • 10
  • 21