PHP 最容易使用的 ORM 框架是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/220229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the easiest to use ORM framework for PHP?
提问by Zak
I'm looking for Ruby's Active record for PHP. Something that is so simple that I just define my fields, extend the base ORM class, and I get ACID operations for free. I should get default getters and setters without writing any code, but overriding a default getter or setter is as easy as declaring get$fieldName or set$fieldName functions with the behavior I want. Symphony makes you create about 5 files per object, and all defined objects always load as far as I can tell. What is a better alternative? Why is it better? Can you put simple examples in your answers please?
我正在寻找 Ruby 的 PHP 活动记录。一些非常简单的事情,我只需定义我的字段,扩展基础 ORM 类,然后我就可以免费获得 ACID 操作。我应该在不编写任何代码的情况下获得默认的 getter 和 setter,但是覆盖默认的 getter 或 setter 就像使用我想要的行为声明 get$fieldName 或 set$fieldName 函数一样简单。Symphony 使您可以为每个对象创建大约 5 个文件,并且据我所知,所有定义的对象始终会加载。什么是更好的选择?为什么更好?你能在你的答案中放一些简单的例子吗?
Doctrine is another ORM I've looked at besides symphony . There also you need to create yaml files that describe your data structures. The database already defines this stuff. What will just read my table defs without having to generate and store config files everywhere?
除了 Symphony 之外,Doctrine 是我看过的另一个 ORM。您还需要创建描述数据结构的 yaml 文件。数据库已经定义了这些东西。什么只会读取我的表 defs 而不必在任何地方生成和存储配置文件?
采纳答案by jakber
Both CodeIgniter (http://codeigniter.com/user_guide/database/active_record.html) and its PHP5 only fork Kohana (http://docs.kohanaphp.com/libraries/orm) contain implementations of the ActiveRecord pattern.
CodeIgniter ( http://codeigniter.com/user_guide/database/active_record.html) 和它的 PHP5 only fork Kohana ( http://docs.kohanaphp.com/libraries/orm) 都包含 ActiveRecord 模式的实现。
回答by dcousineau
I'm a big fan of Doctrinewhich is a full featured ORM that will be replacing Propel as Symfony's default ORM.
我是Doctrine 的忠实粉丝,它是一个功能齐全的 ORM,它将取代 Propel 作为 Symfony 的默认 ORM。
It's got your basic ORM stuff you'd expect along with a full featured query builder that I've found to be wonderful.
它包含您期望的基本 ORM 内容以及我发现非常棒的全功能查询构建器。
It comes with a full suite of command line tools to manage your databases. For example, you can create your schemas and fixtures in YAML, have Doctrine generate classes based on your Schema, create the database, create the schema based on the models, then populate the database with your fixtures all with a single ./doctrine build-all-reload.
它带有一整套命令行工具来管理您的数据库。例如,您可以在 YAML 中创建架构和夹具,让 Doctrine 基于您的架构生成类,创建数据库,基于模型创建架构,然后使用单个./doctrine build-all-reload.
It also includes support for database migrations and recently updatedthe migrations to automatically diff and generate your migration models.
它还包括对数据库迁移的支持和最近更新的迁移以自动区分和生成迁移模型。
As per your doctrine complaints, you can run a command ./doctrine generate-models-dbor ./doctrine generate-yaml-dbto automatically create models and yaml files respectively from your current database setup.
根据您的原则投诉,您可以运行命令./doctrine generate-models-db或./doctrine generate-yaml-db分别从当前数据库设置自动创建模型和 yaml 文件。
Other niceties include "Behaviors" which makes life much easier when implementing certain, well, behaviors in your schema. For example you can add the "Timestampable" behavior to your class file. Doctine automatically adds a 'created_at' and 'updated_at' column, populates them, and every $object->save()you run automatically updates the 'updated_at' column. More complex behaviors include i18n, table versioning, and trees (though really only NestedSet).
其他细节包括“行为”,它使在您的模式中实现某些行为时更容易。例如,您可以将“Timestampable”行为添加到您的类文件中。Doctine 会自动添加“created_at”和“updated_at”列,填充它们,并且每次$object->save()运行都会自动更新“updated_at”列。更复杂的行为包括 i18n、表版本控制和树(尽管实际上只是 NestedSet)。
Personally I've been extremely enamored with Doctrine and rave about it every chance I get.
就我个人而言,我对 Doctrine 非常着迷,并且一有机会就对它赞不绝口。
回答by dcousineau
I use a little known orm layer called redbean. you can find it here : http://www.redbeanphp.com. its absolutely unique in the sense that it just creates tables columns and indexes all by itself without any configuration files at all. I find it to be a huge timesaver!
我使用了一个鲜为人知的名为 redbean 的 orm 层。你可以在这里找到它:http: //www.redbeanphp.com。从某种意义上说,它绝对是独一无二的,它只是自己创建表列和索引,根本没有任何配置文件。我发现它可以节省大量时间!
回答by SchizoDuckie
I've created my own, without the bloat. (Though i need to update my on-site sources)
我已经创建了自己的,没有膨胀。(虽然我需要更新我的现场资源)
I created it with exactly the points in mind you mention: no dozens of xml files, no huge framework, just simple constructors with database to property mappigns and it does your basic CRUD / Find / Join stuff. For most of the stuff i do, i don't even need to write custom queries.
我完全按照您提到的要点创建了它:没有几十个 xml 文件,没有庞大的框架,只有带有数据库到属性映射的简单构造函数,它可以完成您基本的 CRUD/查找/连接。对于我所做的大部分事情,我什至不需要编写自定义查询。
I've written all of this before on my sitealso, make sure to check out the basic examplesto get the idea of it.
我之前也在我的网站上写过所有这些,请务必查看基本示例以了解它。
The next version i'll release comes with working one-line join on join on join (to walk a 'path' through your database), ini based database settings, cross database support, super-simple database abstraction and a standard logger that falls back to SQLite if your database is down.
我将发布的下一个版本带有在 join on join 上工作的单行连接(通过你的数据库走一条“路径”)、基于 ini 的数据库设置、跨数据库支持、超级简单的数据库抽象和一个标准的记录器如果您的数据库已关闭,请返回 SQLite。
Just give a shout if you're interested in the updates, i'll put a rush to it then.
如果您对更新感兴趣,请大喊大叫,然后我会尽快处理。
Oh yeah and don't forget, there's also nice visual scaffold generator called Pork.Generator. It tries to analyze your database structure and find 1:1 1:many and many:many relations, and can automatically generate the classes for you :-)

(source: schizofreend.nl)
哦,是的,别忘了,还有一个很好的视觉脚手架生成器,叫做Pork.Generator。它尝试分析您的数据库结构并找到 1:1 1:many 和 many:many 关系,并可以自动为您生成类
: -) (来源:schizofreend.nl)
回答by Bill Karwin
Zend_Db_Tableand Zend_Db_Table_Roware fairly good at what you're describing. You don't need any configuration file, most metadata is "discovered" from the database itself.
Zend_Db_Table和Zend_Db_Table_Row非常适合您所描述的内容。您不需要任何配置文件,大多数元数据都是从数据库本身“发现”的。
Technically these classes don't implement the ActiveRecord pattern. Instead, they implement the Table Data Gatewayand Row Data Gatewaypatterns. Together, these offer similar value as ActiveRecord, and in some ways are more flexible than ActiveRecord.
从技术上讲,这些类没有实现 ActiveRecord 模式。相反,它们实现了表数据网关和行数据网关模式。总之,这些提供了与 ActiveRecord 相似的价值,并且在某些方面比 ActiveRecord 更灵活。
But as with any ORM, there are inevitably some SQL queries and operations that you can't do through the OO interface. No ORM can serve as one-stop shopping.
但是与任何 ORM 一样,不可避免地存在一些您无法通过 OO 接口执行的 SQL 查询和操作。没有 ORM 可以作为一站式购物。
Footnote: I worked on the Zend Framework project for a little over a year, especially on the Zend_Db component. But I don't work for them anymore.
脚注:我在 Zend Framework 项目上工作了一年多,尤其是 Zend_Db 组件。但我不再为他们工作了。
回答by Frantisek Troster
I would recommend Doctrine with Symfony. Eventhough there is more to learn you will find it has the features you will need once the project grows (CRUD, Form framework, Record Templates, DQL, Plugin support, Behaviours). Both projects have very active community and you shouldn't find yourself in a dead end, because most of your questions have been already answered in official tutorials or in a forum.
我会推荐使用 Symfony 的 Doctrine。尽管还有更多东西需要学习,但您会发现它具有项目发展后所需的功能(CRUD、表单框架、记录模板、DQL、插件支持、行为)。这两个项目都有非常活跃的社区,你不应该发现自己陷入了死胡同,因为你的大部分问题已经在官方教程或论坛中得到了解答。
If you don't like database definitions in YAML, you can always use ORM Designeror MySQL Workbench.
如果您不喜欢 YAML 中的数据库定义,您可以随时使用ORM Designer或 MySQL Workbench。
回答by Frantisek Troster
Check Maintainable framework. Although I prefer code generation over ActiveRecord (runtime reflection), I found Maintainable framework easy to use especially in terms of ORM features.
检查可维护框架。尽管与 ActiveRecord(运行时反射)相比,我更喜欢代码生成,但我发现可维护框架易于使用,尤其是在 ORM 功能方面。
http://framework.maintainable.com/mvc/3_model.php#c3.7
http://framework.maintainable.com/mvc/3_model.php#c3.7
If you want a framework based on code generation, try QCodo. Whatever dcousineau said for Doctrine, I can say for Qcodo. This is an event driven full-fledged framework imitating .NET/Delphi. However you can just code generation feature and find ways to dissociate your generated classed from the rest of the framework. Thus, you can embed generated classed within other frameworks.
如果你想要一个基于代码生成的框架,试试 QCodo。无论 dcousineau 对 Doctrine 说了什么,我都可以对 Qcodo 说。这是一个模仿 .NET/Delphi 的事件驱动的成熟框架。但是,您可以只编写代码生成功能,并找到将生成的类与框架的其余部分分离的方法。因此,您可以将生成的类嵌入到其他框架中。
回答by Vance Lucas
Another option that follows Ruby DataMapper's implementation is phpDataMapper. It's obviously a Data Mapper instead of an ActiveRecord :).
遵循 Ruby DataMapper 实现的另一个选项是phpDataMapper。它显然是一个数据映射器而不是一个 ActiveRecord :)。
回答by Alex Weinstein
I recommend QCubed. It's an incredibly powerful PHP5-only ORM framework that focuses on code generation, UI scaffolding, and rapid application development. Take a look at the training videos: http://qcu.be/content/video-screencasts
我推荐QCubed。它是一个非常强大的仅 PHP5 的 ORM 框架,专注于代码生成、UI 脚手架和快速应用程序开发。看看培训视频:http: //qcu.be/content/video-screencasts
回答by BIOHAZARD
Pros
优点
- Generates/Modifies databases/tables/fields/various table/field attributes on the fly.
- Needs no installation.
- Has no config at all.
- Needs only to include library and specify db link parameters to start work.
- It has built in localization support.
- It has various cache levels and allows to extend cache engine.
- It works with many various database connections same time.
- It can make relations between tables located in other database servers.
- It extracts table/field attributes using class and variable doc comments.
- 动态生成/修改数据库/表/字段/各种表/字段属性。
- 无需安装。
- 根本没有配置。
- 只需要包含库并指定数据库链接参数即可开始工作。
- 它内置了本地化支持。
- 它具有各种缓存级别并允许扩展缓存引擎。
- 它同时适用于许多不同的数据库连接。
- 它可以在位于其他数据库服务器中的表之间建立关系。
- 它使用类和变量文档注释提取表/字段属性。
Cons
缺点
- It works only with objects. i.e. you must have defined class and have instance of class to save load or etc.
- It has no site but has examples folder.
- 它仅适用于对象。即您必须定义类并具有类的实例以保存负载等。
- 它没有站点,但有示例文件夹。

