Search code examples
arrayssymfony1yamlassociative

How to get and use associative array from YAML to action in Symfony?


Ive got in app.yml some configration data, and I want to foreach them in action. I try do this by get them by sfConfig::get('app_datas') but it fails. Lets show them in details:

YAML:

all:
  datas:
    foo: bar
    foo2: bar2

and in the actions.class.php I try use this code:

foreach (sfConfig::get('app_datas') as $key => $value) {

    echo "key $key has value $value";

}

it doesnt work because sfConfig::get('app_datas') is NULL, how simly get it?


Solution

  • When Symfony loads app.yml config files, it only stores the 2nd level down. So you can't access app_datas directly. If you want to get an array containing foo and foo2, make a YAML file like:

    all:
      datas:
        baz:
          foo: bar
          foo2: bar2
    

    You can then do sfConfig::get('app_datas_baz') which will be an array containing foo and foo2 as keys.

    On Edit: kuba's way is better than a dummy; forgot you could do that.