YAML可以继承吗?

发布于 2024-09-05 23:11:48 字数 1156 浏览 1 评论 0原文

这个问题涉及很多 symfony,但对于只知道 YAML 而不懂 symfony 的人来说应该很容易理解。

我的 symfony 模型分为三个步骤:首先,我在 MySQL 中创建表。其次,我运行 symfony 命令 (symfonydoctrine:build-schema) 将表结构转换为 YAML 文件。第三,我运行另一个 symfony 命令 (symfonydoctrine:build-model) 将 YAML 文件转换为 PHP 代码。

问题是:数据库中有一些表我不希望出现在我的 symfony 代码中。例如,假设我有两个表:一个名为 my_table,另一个名为 wordpress。我最终得到的 YAML 文件可能如下所示:

MyTable:
  connection: doctrine
  tableName: my_table
Wordpress:
  connection: doctrine
  tableName: wordpress

很好,除了 wordpress 表与我的 symfony 模型无关。结果是,每次我对数据库进行更改并生成此 YAML 文件时,我都必须手动删除 wordpress。真烦人!

我希望能够创建一个名为 baseConfig.php 的文件或如下所示的文件:

$config = array(
  'MyTable' => array(
    'connection' => 'doctrine',
    'tableName' => 'my_table',
  ),
  'Wordpress' => array(
    'connection' => 'doctrine',
    'tableName' => 'wordpress',
  ),
);

然后我可以有一个名为 config.php 的单独文件或可以对基本配置进行修改的文件:

unset($config['Wordpress']);

所以我的问题是:有什么方法可以将 YAML 转换为可执行的 PHP 代码(而不是像 sfYaml::load() 那样将 YAML 加载到 PHP 代码中)来实现这种事情?或者是否有其他方法来实现 YAML 继承? 谢谢, 贾森

This question involves a lot of symfony but it should be easy enough for someone to follow who only knows YAML and not symfony.

My symfony models come from a three-step process: First, I create the tables in MySQL. Second, I run a symfony command (symfony doctrine:build-schema) to convert my table structure into a YAML file. Third, I run another symfony command (symfony doctrine:build-model) to convert the YAML file into PHP code.

Here's the problem: there are some tables in the database that I don't want to end up in my symfony code. For example, let's say I have two tables: one called my_table and another called wordpress. The YAML file I end up with might look like this:

MyTable:
  connection: doctrine
  tableName: my_table
Wordpress:
  connection: doctrine
  tableName: wordpress

That's great except the wordpress table has nothing to do with my symfony models. The result is that every single time I make a change to my database and generate this YAML file, I have to manually remove wordpress. It's annoying!

I'd like to be able to create a file called baseConfig.php or something that looks like this:

$config = array(
  'MyTable' => array(
    'connection' => 'doctrine',
    'tableName' => 'my_table',
  ),
  'Wordpress' => array(
    'connection' => 'doctrine',
    'tableName' => 'wordpress',
  ),
);

And then I could have a separate file called config.php or something where I could make modifications to the base config:

unset($config['Wordpress']);

So my question is: is there any way to convert YAML into executable PHP code (as opposed to load YAML INTO PHP code like what sfYaml::load() does) to achieve this sort of thing? Or is there maybe some other way to achieve YAML inheritance?
Thanks,
Jason

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

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

发布评论

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

评论(2

夜吻♂芭芘 2024-09-12 23:11:49

另一种方法:

我不确定 Symphony 是否会让您这样做,但是 Doctrine 支持手工构建的架构文件。您需要手动保持同步,但是根据您对表的更改程度,这可能是可以管理的。

An alternative approach:

I'm not sure if Symphony will let you do this, but Doctrine supports hand-built schema files. You'll need to keep things in sync manually, but depending on how much you change your tables, this might be managable.

甜心小果奶 2024-09-12 23:11:48

正确答案(据我所知):不。

顺便说一句,这是我最终编写的插件:
http://jasonswett.net/jsdoctrineschemaoverriderplugin/

Correct answer (as far as I can tell): no.

By the way, here's the plugin I ended up writing:
http://jasonswett.net/jsdoctrineschemaoverriderplugin/

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