Symfony 中的 YML 文件 - 您能否获得向下超过 1 级的关联数组?

发布于 2024-09-18 23:22:32 字数 726 浏览 5 评论 0原文

我有大量数据想要存储在 /apps/frontend/modules/builder/config/module.yml 中。

我让它看起来像:

all:
  series_options:
    compact:
      name: Compact
      description: Something small.
      enabled: 1
    large:
      name: Large
      description: Bit bigger.
      enabled: 0

在 actions.class 中,如果我写这个:

sfConfig::get('mod_builder_series_options_compact');

我得到这个

Array
(
  [name] => Compact
  [description] => Something small.
  [enabled] => 1
)

完美。但我想这样写:

sfConfig::get('mod_builder_series_options');

这给出了NULL

有什么方法可以让我将完整的关联数组返回到其完整深度,以便我可以迭代不同的选项?

I have a load of data I want to store in /apps/frontend/modules/builder/config/module.yml.

I have it looking something like:

all:
  series_options:
    compact:
      name: Compact
      description: Something small.
      enabled: 1
    large:
      name: Large
      description: Bit bigger.
      enabled: 0

In the actions.class if I write this:

sfConfig::get('mod_builder_series_options_compact');

I get this

Array
(
  [name] => Compact
  [description] => Something small.
  [enabled] => 1
)

Perfect. But I want to write this:

sfConfig::get('mod_builder_series_options');

Which gives NULL.

Is there any way I can get this to return the full associative array to its full depth so I can iterate through the different options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心↖ 2024-09-25 23:22:32

您可以在其名称前添加一个带有点的级别,以强制数组处于特定级别:

all:
  .options:
    series_options:
      compact:
        name: Compact
        description: Something small.
        enabled: 1
      large:
        name: Large
        description: Bit bigger.
        enabled: 0

现在您应该能够通过以下方式访问您的设置:

sfConfig::get('mod_builder_series_options');

请记住,模块配置只能在其定义的模块中访问。

You can add a level with a dot before its name to force array on certain level:

all:
  .options:
    series_options:
      compact:
        name: Compact
        description: Something small.
        enabled: 1
      large:
        name: Large
        description: Bit bigger.
        enabled: 0

Now you should be able to access your settings with:

sfConfig::get('mod_builder_series_options');

Remember that module configuration is only accessible in the module it is defined.

罪#恶を代价 2024-09-25 23:22:32

典型的是,当我一发布答案时,答案就打在我脸上......

$series = sfYaml::load('../apps/frontend/modules/builder/config/module.yml');  
print_r($series);die;

返回:

Array
(
  [all] => Array
    (
        [series_options] => Array
            (
                [compact] => Array
                    (
                        [name] => Compact
                        [description] => Something small.
                        [enabled] => 1
                    )

                [large] => Array
                    (
                        [name] => Large
                        [description] => Bit bigger.
                        [enabled] => 0
                    )
            )

    )

)

猜测 sfConfig 并不是真正用于此目的,而 sfYaml 看起来确实是这样!

希望这对其他人有帮助!

Typical, as soon as I resort to posting the answer hits me in the face...

$series = sfYaml::load('../apps/frontend/modules/builder/config/module.yml');  
print_r($series);die;

Returns:

Array
(
  [all] => Array
    (
        [series_options] => Array
            (
                [compact] => Array
                    (
                        [name] => Compact
                        [description] => Something small.
                        [enabled] => 1
                    )

                [large] => Array
                    (
                        [name] => Large
                        [description] => Bit bigger.
                        [enabled] => 0
                    )
            )

    )

)

Guessing that sfConfig wasnt really meant for this purpose, where sfYaml certainly looks like it was!

Hope this helps someone else!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文