Masha
Masha

Reputation: 857

php symfony setting nested parameters in container

In my php symfony app I got the following parameters.yml:

my_param: some_value
my_nested:
    param: some_value2 //4 spaces here, need to set only this param
    param2: some_value3 // this should NOT be changed

I can set the parameter in a container with the following code:

$container->setParameter('my_param', $some_value);

it works fine, but I need to set a nested parameters, like this:

$container->setParameter('my_nested.param', $some_value);

I get an error saying that it cannot set nested param. Any ideas how to fix that? my_nested.param2 (and other nested params here) should not be changed. Thank you.

Upvotes: 0

Views: 927

Answers (2)

Magd Kudama
Magd Kudama

Reputation: 3479

You need to use an array-like structure, like this:

$container->setParameter('my_nested', [
  'param' => $some_value_2,
]);

Have a look here: https://symfony.com/doc/current/service_container/parameters.html

An example to change a nested parameter:

$container->setParameter('my_nested', [
  'top' => [
    'nested' => 'a',
  ]
]);

$parameter = $container->getParameter('my_nested');
$parameter['top']['nested'] = 'b';

$container->setParameter('my_nested', $parameter);

Upvotes: 1

Yassine CHABLI
Yassine CHABLI

Reputation: 3754

you can define the value like that :

the parameters.yml :

    parameters:        
        my_param: some_value
        my_nested.param: some_value2

you can get the value as you show in your description , but modify this value is impossible . symfony will notify you with the error :

Impossible to call set() on a frozen ParameterBag.

Upvotes: 1

Related Questions