简体   繁体   English

您如何将数据库中的枚举与您的 PHP 代码同步?

[英]How do you synchronize enums in DB with your PHP code?

A db with several hundred tables will usually have more then few enums/sets defined in it.具有数百个表的数据库通常会在其中定义更多的枚举/集。
How do you synchronize you PHP code with the enum values, to avoid using string literals in your code (which we all know it just seems easier, but in the long run causes stupid bugs).你如何将你的 PHP 代码与枚举值同步,以避免在你的代码中使用字符串文字(我们都知道这看起来更容易,但从长远来看会导致愚蠢的错误)。
The only good solution I can think of is using, during the build of the project, a process which will generate constants for all the enums in the DB, or, create and compile a PHP extension with those values.我能想到的唯一好的解决方案是在项目构建期间使用一个过程,该过程将为数据库中的所有枚举生成常量,或者使用这些值创建和编译 PHP 扩展。
Any different, better solutions out there?有什么不同的、更好的解决方案吗?

As Plato would say: either PHP and SQL are independent, or SQL is generated from PHP, or PHP is generated from SQL, or both are generated from the same source.正如柏拉图所说:要么PHP 和SQL 是独立的,要么从PHP 生成SQL,要么从SQL 生成PHP,或者两者都从同一源生成。

The first is usually unacceptable if there's a lot of changes happening, but otherwise quite stable.如果发生很多变化,第一个通常是不可接受的,但在其他方面非常稳定。

The second is what happens with a lof of recent solutions that manage your SQL from the PHP side (and gracefully handle schema changes from the PHP side as well).第二个是一些最近的解决方案,这些解决方案从 PHP 端管理您的 SQL(并且从 PHP 端优雅地处理架构更改)。 This means your mad SQL ninja skills might be useless and frustrating .这意味着您疯狂的 SQL 忍者技能可能毫无用处且令人沮丧

The third is what you suggest.第三个是你的建议。 It's fine as long as you can manage the "generated during build" part without stepping on the toes of your source control system and your package system.只要您可以管理“在构建期间生成”的部分,而无需踩到源控制系统和包系统的脚趾,就可以了。 Having a clean build system helps (which is somewhat unusual in PHP).拥有一个干净的构建系统会有所帮助(这在 PHP 中有点不寻常)。

The fourth involves adapting an existing database modeling tool to generate both the SQL schema and the PHP code.第四个涉及调整现有的数据库建模工具以生成 SQL 模式和 PHP 代码。 This way, you would simply work with the tool once every two weeks, generate both PHP and SQL, and commit the result.这样,您只需每两周使用一次该工具,生成 PHP 和 SQL,然后提交结果。 Adapting the tool, however, can be quite nasty (then again, you can try to look for an XML export and work from there).然而,调整该工具可能非常麻烦(然后,您可以尝试寻找 XML 导出并从那里开始工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM