使用 Doctrine for PHP 的优点?

发布于 2024-10-01 05:00:13 字数 324 浏览 4 评论 0原文

刚刚遇到 Doctrine 项目,它有一个对象关系映射器和一个数据库抽象层。 Doctrine 提供了哪些其他 PHP 抽象层没有提供的功能?除了通过用 Doctrine 查询语言编写的查询来获取对象之外,ORM 还能有什么实际用途呢?您真的想用这种查询语言来开发整个 Web 应用程序吗?表现好吗?

总的来说,在 Doctrine 上构建应用程序是否更容易维护和理解?它是否过度设计,并且构建在适合中小型项目的抽象层上吗? (<50 个 GUI 屏幕),而不是直接使用 MySQL。

Just came across the Doctrine Project which has an Object Relational Mapper and a DB Abstraction Layer. What does Doctrine provide that other PHP abstraction layers don't? And what practical use can you put the ORM to, apart from fetching objects via queries written in Doctrine Query Language? Is the query language really something you want to develop an entire web app in? Does it perform well?

On the whole does building an app on Doctrine make it easier to maintain and understand? Is it over-engineered, and is building on an abstraction layer sensible for small-medium size projects? (<50 GUI screens), as opposed to directly working with MySQL.

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

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

发布评论

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

评论(3

孤星 2024-10-08 05:00:13

Doctrine 提供了哪些其他 PHP 抽象层没有提供的功能?

  1. 实现 DataMapper 模式而不是 ActiveRecord。
  2. 支持架构的注释、XML 和 YAML 。
  3. 使用DQL。
  4. 使用 PHP 5.3+ 的优点。
  5. 速度快,社区大。
  6. 除了ORM还有ODM。

您真的想用这种查询语言来开发整个网络应用程序吗?

只有负责维护业务对象的应用程序的一部分应该知道 Doctrine 的存在。这部分不一定是 100% 基于教义的。

总的来说,在 Doctrine 上构建应用程序是否更容易维护和理解?

确实。代码更容易阅读、理解和维护。

它是否过度设计,对于中小型项目是否合理?

事实上,教义的基本原理非常简单。对于小型、中型甚至一些大型应用程序来说,这是一个非常好的选择。


教义并不能解决所有问题,有时还存在一些问题。然而对于典型的任务来说它非常有用。恕我直言,这是目前 PHP 最好的 ORM/ODM。

What does Doctrine provide that other PHP abstraction layers don't?

  1. Implements DataMapper pattern rather ActiveRecord.
  2. Supports annotations, XML and YAML for schema.
  3. Uses DQL.
  4. Uses benefits of PHP 5.3+.
  5. Is fast and has large community.
  6. Except ORM there is ODM.

Is the query language really something you want to develop an entire web app in?

Just a part of the application responsible for maintaining business-objects should be aware of the existence of Doctrine. And that part doesn't have to be 100% Doctrine-based.

On the whole does building an app on Doctrine make it easier to maintain and understand?

Definitely. The code is easier to read, understand and maintain.

Is it over-engineered, and is it sensible for small-medium size projects?

Actually Doctrine is quite simple in its fundamentals. And it's a very good choice for small, medium and even some large applications.


Doctrine isn't the answer for everything and sometimes it's a little problematic. However for typical tasks it's extremely useful. IMHO the best ORM/ODM for PHP at this moment.

会发光的星星闪亮亮i 2024-10-08 05:00:13

我想对克罗津的回答补充几点,但遗憾的是无法发表评论。它们是:

  • Doctrine 不使用魔术方法 __get() 和 __set() 来访问实体属性,所有实体属性都应该有 getter / setter。这提高了 IDE 代码的完成度,并​​且您不需要一直查看 DB 表结构。
  • Doctrine 完全将您从真实的表字段名称中抽象出来。将实体属性映射到数据库字段后,您可以在任何地方使用属性名称。表名也一样。
  • Doctrine 使用存储库模式来隐藏获取实体的详细信息。
  • Doctrine 采用“代码优先”的方法,因此您可以先创建实体,然后自动为它们生成数据库。相反的情况也是可能的。
  • Doctrine 具有强大的查询构建器,因此您可以利用构建器模式进行带有条件部分的查询。
  • Doctrine 使用外键和约束来执行级联操作并保持数据一致。
  • Doctrine 的 UnitOfWork 是一个非常伟大和聪明的东西,在其他 php ORM 中没有类似的东西,

恕我直言,目前 Doctrine 提供了所有可用 php ORM 中最好的 IDE 代码完成支持和数据库层抽象。它没有过度设计并遵循坚实的原则。

I'd like to add a few points to Crozin's answer, but unfortunately can't comment it. Here they are:

  • Doctrine does not use magic methods __get() and __set() to access entity attributes, all the entity attributes should have getter / setter. This improves IDE code completion and you do not need to look at DB table structure all the time.
  • Doctrine completely abstracts you from real table field names. Once you mapped entity properties to DB fields - you use property names everywhere. Same for table names.
  • Doctrine uses repository pattern which hides the details of obtaining entities.
  • Doctrine utilizes "code first" approach, so you can create entities first and then generate database for them automatically. Reverse case is also possible.
  • Doctrine has powerful query builder, so you can utilize builder pattern for queries with conditional parts.
  • Doctrine uses foreign keys and constraints to perform cascade actions and keep data consistent.
  • Doctrine's UnitOfWork is a pretty great and smart thing, which has no analogue in other php ORMs

IMHO at the moment doctrine provides best IDE code completion support and DB layer abstraction among all php ORMs available. It is not over-engineered and follows SOLID principles.

淤浪 2024-10-08 05:00:13

我想对 GerKirill 的回答补充一点。恕我直言,缺乏对魔法 getter/setter 方法的支持是一个弱点,而不是一个优点。如果您曾经滚动浏览过几十页相同的 getter/setter,您会意识到这些方法是对空间的巨大浪费(更不用说编译时间了)。没有人会意外地设置对象变量,并且 setter 不会阻止您这样做......当您想要更改属性时,您只需调用 setter(setter 如何“保护”属性 - 如果您是如果你会犯一个错字并直接设置错误的属性值,你也会犯同样的错字并调用错误的setter)。除了获取或设置属性之外,setter 或 getter 很少做任何事情。如果您必须执行一些特殊操作来设置或获取属性,则该属性应该是一个方法(请参阅 http://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html),否则你应该重构您的代码,或者您应该调用属性验证函数(通常在创建对象时)。这是困扰面向对象世界的无可争议的真理之一。在发布标准的公认回复之前请考虑一下。

I'd like to add a point to GerKirill's answer. The absence of support for magic getter/setter methods is a weakness, IMHO, not a strength. If you've ever scrolled through dozens of pages of identical getters/setters, you will realize that these methods are a huge waste of space (not to mention compile time). No-one accidentally sets an object variable, and a setter does not prevent you from doing that ... when you want to change the property, you just call the setter (how does a setter "protect" the property -- if you are going to make a typo and directly set the wrong property value, you will make the same typo and call the wrong setter). And it is very rare for a setter or getter to do anything other than get or set a property. If you have to do something special to set or get a property, that property should be a method (see http://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html), or you should be refactoring your code, or you should be calling a property validation function (generally at the time the object is created). This is one of those un-challenged truisms that plague the OO world. Think about it before you post the standard received-wisdom reply.

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