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

quardas picture quardas · Oct 16, 2010 · Viewed 12.1k times · Source

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?

Answer

Jakub Zalas picture Jakub Zalas · Oct 16, 2010

If you want to access first level as an array you can introduce dummy level in between, just like @jeremy suggested. Prefix it with a dot if you don't want it to actually appear in config the variable names:

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

Now you should be able to access your data with:

foreach (sfConfig::get('app_datas') as $key => $value) 
{
  echo "key $key has value $value";
}