Doctrine 在 Active Record 之上添加了什么 - CodeIgniter?
我真的很喜欢 CodeIgniter 的 Active Record,它非常好地允许我进行所有需要的数据库查询。
但我也一直在阅读有关 ORM 的内容,例如 Doctrine。当我阅读 Doctrine 的文档时,它的使用似乎不像 Active Record 那样清晰,而且我看不出是什么让它变得更好(如果是的话)。
Doctrine 允许什么是 Active Record 无法实现的? Doctrine 是否能让同样的工作更快、更容易、更好?或者它能做 Active Record 不能做的事情吗?
最好的是人们可以发布任务示例来展示我们正在讨论的内容。
谢谢, 马修
I really like CodeIgniter's Active Record and how nicely it allows all my needed database queries.
But I've also been reading about ORMs like Doctrine. When I read Doctrine's documentation, it does not seem as clear to use as Active Record, and I can't see what makes it better (if it is).
What does Doctrine allow that is not possible with Active Record? Does Doctrine make the same job faster, easier, better? Or does it do things Active Record cannot do?
Best would be if people could post examples of tasks showing what we're talking about.
Thanks,
Matthew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,你在谈论什么学说,1 还是 2?
有很大的不同。两者唯一的共同点是它们都是成熟的 ORM。否则两者之间确实没有任何联系。
原则 1 基于ActiveRecords,原则 2 基于数据映射器模式。
两者可以做相同的事情,但两者之间存在一些显着差异。
一般来说,数据映射器不太“对开发人员友好”,但应该具有更好的性能。为什么?其实这很简单。通过活动记录,每个实体都知道其自身“周围”的一切、与其他实体的关系等。通过数据映射器,实体是愚蠢且轻量级的,有一个中央实体(Doctrine2 中的 EntityManager/UnitOfWork)来处理所有关系映射。因此,就内存使用和性能而言,数据映射器应该更快。
Doctrine 的人说 Doctrine2 比 Doctrine1 至少快 50%(还有其他差异,不仅仅是设计模式)。
如果您愿意,您甚至可以通过 Doctrine2 数据映射器实现 ActiveRecords。请查看这篇博客文章。我仅在开发阶段使用这种方法,以保留尽可能少的代码。一旦投入生产,我将终止额外的 ActiveRecords 层,并回滚到 Doctrine2 的默认数据映射器。
所以结论是,您可以使用两者完成所有操作,但同样,您可以说您可以使用原始 SQL 完成所有操作。如果您是 ORM 世界的初学者,我建议使用 ActiveRecords,因为它很简单并且(通常)需要更少的代码。另一方面,如果您正在构建大型、复杂的模型,我认为数据映射器是更好的选择。
也许我理解错了,但这就是我的理解。
至于CodeIgniters ActiveRecords和Doctrine(1或2)之间的比较,我真的不好说,因为我从来没有使用过CodeIgniter。我确信的一件事是,Doctrine 比 CodeIgniters 默认的 ORM 有更多的功能。例如:结果水化、继承(单表、类表)、预取、延迟加载、额外延迟加载、扩展、行为、优化、代理、日期时间处理...这是一个庞大且成熟的 ORM,包含大量内容功能,而我对任何“默认框架 ORM”的经验是,它们的主要目标是尽可能简单,因此新手可以很容易地掌握它。 Doctrine 是一头强大的野兽,并且肯定可以比内置的 CodeIgniter ORM 更有效和/或逻辑上更正确的方式做很多事情。缺点是,它需要更多的时间来学习和编码,而且它是一个巨大的库,有数千个文件,因此与更轻的替代方案相比,仅仅让所有东西运行就会增加一些开销。
First of all, what Doctrine are you talking about, 1 or 2 ?
There is a huge difference. The only thing that the two have in common is that they are both full-fledged ORM-s. Otherwise there really isn't any connection between the two.
Doctrine 1 is based on ActiveRecords, Doctrine 2 is based on Data mapper pattern.
Both can do same things, but there are some significant differences between the two.
Generally speaking Data mapper is less "developer-friendly" but should have better performance. Why? Actually it is pretty simple. With active records each entity knows everything "around" itself, relation with other entities etc. With data mapper, entities are dumb and lightweight, there is a central entity (EntityManager/UnitOfWork in Doctrine2) which handles all the relation mapping. So in terms of memory usage and performance Data mapper should be faster.
The Doctrine guys say that Doctrine2 is a least 50% faster that Doctrine1 (there are other differences too, not just the design pattern).
If you feel up for it, you can even implement ActiveRecords over Doctrine2 data mapper. Look at this blog post. I'm using this approach just for the development phase, to keep as little code as possible. Once it gets into production I will kill the additional ActiveRecords layer, and rollback to the default data mapper of Doctrine2.
So the conclusion is that you can do everything with both, but in the same way you could say that you can do everything with raw SQL. If you are a beginner in the ORM world, I would suggest going with ActiveRecords, because it is simple and (usually) requires less code. On the other hand, if you are building a large, complex model, I think data mapper is the better option.
Maybe I got something wrong, but this is how I understood it.
As for the comparison between CodeIgniters ActiveRecords and Doctrine (1 or 2), I can't really say, because I never used CodeIgniter. One thing I am sure of, Doctrine has a lot more features than CodeIgniters default ORM. For example: result hydration, inheritance (single table, class table), prefetching, lazy loading, extra lazy loading, extensions, behaviours, optimization, proxies, datetime handling... It is a massive and full-fledged ORM with a lot of features, while my experience with any "default framework ORM" is that their main goal is to be simple as possible, so a newbie can get a hang of it very easily. Doctrine is a mighty beast, and for sure can do a lot of things in a more efficient and/or logically more correct way than the built in CodeIgniter ORM. The downside is, that it takes more time to learn and code, and it is a huge library, with thousands of files, so just to get everything running adds some overhead compared to a lighter alternative.
Doctrine 是一个成熟的 ORM,它实现了活动记录模式。 CodeIgniter 的活动记录类是一个基于模式的“修改”版本的查询构建器/数据库包装器。
免责声明:我从未使用过 Doctrine。我会根据我的理解,尽力说明CodeIgniter的active record实现和Doctrine之间的区别。
使用 CodeIgniter 的活动记录类,您可以实现如下所示的模型:
您基本上是使用活动记录方法构建查询。很容易看出每个方法(
where()
、get()
等)如何映射到原始 SQL。与仅$this->db->query()
相比,使用活动记录方法的优点是 CodeIgniter 根据您正在使用的数据库驱动程序编译每个查询。除此之外,CodeIgniter 的活动记录实现并没有真正做太多事情。您需要创建任何查询。我希望我已经说明了活动记录方法与查询生成器的相似之处。请注意,以下示例代码可能不正确。使用 Doctrine,您可能有一个如下所示的模型:
然后,要使用该模型和关联的活动记录功能,您需要执行如下操作:
这只是 Doctrine 功能的皮毛。您可以设置属性的默认值或指定表之间的关系。请注意,我没有编写任何 SQL 或构建查询。我只是设置了对象的属性,然后保存了它。教义负责其余的事情。
请注意,Doctrine 包含其自己的查询构建器,因此在某种程度上它可以执行 CodeIgniter 的活动记录的功能,甚至更多。
使用 Doctrine 类似于 CakePHP 或 Ruby on Rails 的活动记录模式实现。您可以在那里查看以进一步了解该模式。如果您有 CodeIgniter 背景,CakePHP 的示例可能特别容易理解。
为了回答您的其他一些问题,我认为 Doctrine 没有什么比 CodeIgniter 活动记录方法更好的了。它可能更先进,但像任何其他库一样,您希望选择最适合工作的工具。如果您对 CodeIgniter 的活动记录方法感到满意并且您认为不需要高级 ORM,那么请跳过它。
Doctrine is a full-fledged ORM that implements the active record pattern. CodeIgniter's active record class is a query builder/database wrapper that is based on a "modified" version of the pattern.
Disclaimer: I have never used Doctrine. I will try my best to illustrate the differences between CodeIgniter's active record implementation and Doctrine, based on my understanding.
Using CodeIgniter's active record class, you might implement a model like this:
You are basically building the query using the active record methods. It's easy to see how each method (
where()
,get()
, etc) maps to raw SQL. The advantage to using the active record methods as opposed to just$this->db->query()
is that CodeIgniter compiles each query based on the database driver you are using. Other than that, CodeIgniter's active record implementation doesn't really do much. Any queries you need, you'll need to create. I hope I've illustrated how the active record methods are similar to a query builder.Note that that the following sample code may be incorrect. Using Doctrine, you might have a model like this:
Then to use the model and the associated active record functionality, you would do something like this:
This is just scratching the surface in terms of what Doctrine can do. You can set default values for properties or specify relationships between tables. Notice how I didn't write any SQL or build the query. I just set the properties of the object and then saved it. Doctrine takes care of the rest.
Note that Doctrine includes its own query builder, so in a way it does what CodeIgniter's active record does, and more.
Using Doctrine is similar to CakePHP's or Ruby on Rails' implementation of the active record pattern. You could take a look there for additional insight into the pattern. CakePHP's examples might be particularly easy to digest if you're coming from a CodeIgniter background.
To answer some of your other questions, I don't think there's anything that makes Doctrine better than the CodeIgniter active record methods. It may be more advanced, but like any other library, you want to pick the best tool for the job. If you are happy with CodeIgniter's active record methods and you see no need for an advanced ORM, then skip it.