现有 PHP 应用程序需要一个简单的 ORM 或 DBAL
我正在致力于扩展现有的 PHP 应用程序。对我来说不幸的是,现有的应用程序一团糟。这都是带有原始 mysql_* 调用的意大利面条代码。呻吟。我不可能在我要扩展的部分中这样做。
因此,我正在寻找一个简单的 DBAL ORM,我可以轻松地插入并开始使用。所需的功能:
- 它必须适用于现有的数据库模式。最好具有最少的附加配置或没有附加配置。现有的数据库架构与现有的 PHP 代码具有相同的质量(没有合理的命名约定,未规范化等)。我不想花几天时间将数据库模式手动转换为 Doctrine 2 中带注释的对象属性。
- 它必须能够与现有的原始 mysql_* 查询一起工作。我不知道当脚本在背后手动操作数据库中的数据时,像 Doctrine 2 或 Propel 这样的水合 ORM 会如何表现,但我认为这并不漂亮。
- 它必须在 PHP 5.2.x 上运行。我喜欢使用 PHP 5.3,但我对检查现有的 125K 行意大利面条式代码以确保它在 PHP 5.3 上运行没有兴趣。
- 不需要关系。在我需要访问关系数据的少数地方,我很乐意自己调用额外的
find()
或query()
或其他方法。 - 如果它有一些触发器支持(例如
beforeSave
、afterSave
),则会加分。不是必需的,但拥有就好。
编辑:有人让我摆脱了痛苦。我刚刚发现 125K 行意大利面条式代码也改变了数据库模式。例如,在某处添加一个额外的选项,一大堆 ALTER TABLE 语句就会开始运行。我可能可以用这个代码库填满一年的 TheDailyWTF。因此,还有一个要求:
- 必须能够自动应对不断变化的数据库模式(例如添加列)。
我一直在研究一些解决方案,但我不确定它们在满足要求的情况下效果如何。 Doctrine 2、RedBeanPhp 等都需要 PHP 5.3,所以它们已经被淘汰了。有一个适用于 PHP 5.2.x 的 RedBeanPhp 旧版本,但我不知道它是否适用于混乱的现有数据库模式。 NotORM 看起来可以很好地获取数据,但我不知道它是否可以针对现有数据库模式进行配置,以及如何轻松地将数据放回到数据库中。
理想情况下,我想要一些简单的东西。例如:
$user = User::find($id);
$user->name = 'John Woo';
$user->save();
或者:
$articles = ORM::find('article')->where('date' => '2010-01-01');
foreach ($articles as $article) {
echo $article->name;
}
欢迎任何提示甚至替代解决方案!
I am working on extending an existing PHP application. Unfortunately for me, the existing app is a mess. It's all spaghetti code with raw mysql_* calls. Groan. No way that I am going to do that in the parts that I am extending.
So, I am looking for a simple ORM of DBAL that I can easily drop in and start using. Desired features:
- It must work on an existing database schema. Preferably with minimal or no additional configuration. The existing database schema is the same quality as the existing PHP code (no sensible naming conventions, not normalised, etc.). I don't want to spend days converting the database schema manually into annotated object properties a la Doctrine 2.
- It must be able to work alongside the existing raw mysql_* queries. I have no idea how hydrating ORMs like Doctrine 2 or Propel behave when scripts are manually manipulating the data in the database behind their backs, but I assume it's not pretty.
- It must run on PHP 5.2.x. I'd love to use PHP 5.3 but I have zero interest in going over the existing 125K lines of spaghetti code mess to make sure it runs on PHP 5.3.
- Relationships not required. In the few places I need to get to relational data, I'll be happy to call an extra
find()
orquery()
or whatever myself. - Bonus points if it has some trigger support (e.g.
beforeSave
,afterSave
). Not a requirement, but just nice to have.
Edit: Someone put me out of my misery. I just found out that the 125K lines of spaghetti code also changes the database schema. E.g, add an extra option somewhere and a whole slew of ALTER TABLE statements start flying. I could probably fill a year's worth of TheDailyWTF with this codebase. So, one more requirement:
- Must be able to cope with a changing database schema automatically (e.g. adding columns).
I have been looking at a few solutions, but I am unsure how well they would work given the requirements. Doctrine 2, RedBeanPhp and the like all require PHP 5.3, so they are out. There's a legacy version of RedBeanPhp for PHP 5.2.x but I don't know if it would work with a messy, existing database schema. NotORM looks okay for getting data out but I don't know if it can be configured for the existing database schema, and how you can easily put data back into the database.
Ideally I would like something simple. E.g:
$user = User::find($id);
$user->name = 'John Woo';
$user->save();
Or:
$articles = ORM::find('article')->where('date' => '2010-01-01');
foreach ($articles as $article) {
echo $article->name;
}
Any tips or even alternative solutions are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用...
http://github.com/j4mie/idiorm/
它也有一个活动记录实现,形式为巴黎的。
关于您的编辑。 Idiorm 可以应对不断变化的模式,并且语法几乎与您在问题中想要的类型完全匹配。
I use...
http://github.com/j4mie/idiorm/
it has an active record implementation too in the form of Paris.
With regard to your edit. Idiorm copes with changing schemas and the syntax almost exactly matches the type you want in your question.
您对教义的研究程度如何?我正在使用 Doctrine 1.2 来处理这类事情。设置非常容易,允许您从现有架构开始。它会自动找出具有外键约束的表之间的关系。
它具有广泛的触发器和行为支持,因此也可以花费奖励积分,并且它还具有关系支持,因此您无需进行额外的查询。它具有出色的延迟加载功能,并且配备了灵活的查询语言(称为 DQL),使您只需花费一小部分精力即可完成与 SQL 中几乎完全相同的操作。
您的示例将如下所示:
嗯,这在技术上是不可能自动管理的; SQL 数据库根本不会将内容推送回 ORM,因此要更新在后台更改的内容,您需要以某种方式执行附加查询。幸运的是,Doctrine 让这对你来说变得非常容易:
How well did you look into Doctrine? I am using Doctrine 1.2 for these kind of things. Quite easy to setup, allows you to start off with an existing schema. It automatically figures out the relations between tables that have foreign key constraints.
It has extensive trigger and behaviour support, so the bonus points can be spent as well, and it has relational support as well, so your additional queries are not necessary. It has beautiful lazy loading, and it comes with a flexible query language (called DQL) that allows you to do almost exactly the same stuff that you can do in SQL in only a fraction of the effort.
Your example will look like this:
Well, that is technically impossible to auto-manage; a SQL database is simply not pushing back stuff to your ORM, so to update stuff that was changed in the background, you need to perform an additional query one way or the other. Fortunately, Doctrine makes this very easy for you: