6

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
quardas
  • 651
  • 3
  • 10
  • 23

2 Answers2

17

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";
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • way to go, kuba - saved my day on a hard deadline! – Timm Mar 01 '12 at 17:55
  • 2
    Minor correction: the dotted value on the second line (.baz) should have a colon suffix, same as the other lines. (I'll make an edit). – halfer Apr 08 '12 at 14:51
11

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.

Jeremy Kauffman
  • 10,293
  • 5
  • 42
  • 52