0

I'm using Config component on a standalone PHP app, and I would like to have in my configuration file a structure similar to below :

sites:
  site1:
    ftp:
      server: myserver
      name: name
      ...: ...
    database:
      server: myserver
      name: ....

AFAIS, I need the useAttributeAsKey() method on my array for the "site1" node ("sites" node can handle several sites, with name as key).

I tried to set it up this way, but I end up to a "children method not in NodeDefinition". How to set it correctly?

$rootNode
            ->children()
                ->scalarNode(self::REMANENCE_NODE[self::NODE_NAME])
                    ->defaultValue(self::REMANENCE_NODE[self::NODE_DEFAULT_VALUE])
                    ->info('Contains the backup folders max value to keep on defined storages')
                ->end()
                ->arrayNode(self::SITES)
                    ->requiresAtLeastOneElement()
                    ->useAttributeAsKey('name')
                    ->prototype('array')             
                    ->children()
                        ->arrayNode(self::DATABASE)
                        ->end()
                        ->arrayNode(self::FILES)
                        ->end()
                    ->end()
                ->end()

Thanks for your replies, Nicolas

1 Answer 1

0

I finally found the good configuration :

$rootNode
        ->children()
            ->scalarNode(self::REMANENCE_NODE[self::NODE_NAME])
                ->defaultValue(self::REMANENCE_NODE[self::NODE_DEFAULT_VALUE])
                ->info('Contains the backup folders max value to keep on defined storages')
            ->end()
            ->arrayNode(self::SITES)
                ->requiresAtLeastOneElement()
                ->prototype('array')
                    ->children()
                        ->arrayNode(self::DATABASE)
                            ->children()
                                ->scalarNode(self::DATABASE_SERVER[self::NODE_NAME])
                                    ->defaultValue(self::DATABASE_SERVER[self::NODE_DEFAULT_VALUE])
                                    ->info('Contains the database server IP or name')
                                ->end()

This above works for following configuration :

configuration:
  remanence: 5
  sites:
    site_1: # this is the attribute as key
      database:
        server: localhost
        ... : ...

2 things to notice :

  • prototype('array') seems to handle key as attribute in this case.
  • I use PHPStorm which provides informations using PHPDoc and I currently have a underlighted children() method (after prototype one) indicating me warning I mentionned in original question. I will dig deeper later, but it seems to have a mess in here.

This SO post showed me the right way, which is also what suggest official documentation

Cheers,

Nicolas

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.