如何将 ORM 添加到 PHP 遗留项目?

发布于 2024-07-14 19:43:54 字数 601 浏览 8 评论 0原文

我们正在开发一个 PHP 项目,该项目已经开发了 2 年多了,现在团队已经准备好并愿意将开发切换到 ORM。 因为它确实加快了开发速度,并且允许您通过对象进行操作,而无需大多数时候考虑 SQL 代码和数据库表。

我们决定选择 Doctrine ORM,因为它加载了 YAML 数据固定装置- 我们的单元测试非常需要它。

我主要担心的是,使用新的 ORM 框架会降低网站的性能。 我们无法在当前数据库抽象层之间建立共享连接(使用 pg_connect 语法,而不是 PDO 兼容)。 数据库连接机制无法切换为PDO兼容,因为有很多SQL代码与PDO_SQLITE语法不兼容。

因此,据我了解,如果我们开始使用它,数据库连接的数量将会增加一倍。 我不确定数据库服务器是否能够处理这个问题。

在这种情况下,您建议我们做什么?

We're working on a PHP project, which has been in development for more than 2 years, and now the team is ready and feel the willingness to switch the development on to an ORM. Because it really speeds up the development and allow you to operate by Objects and not think in terms of SQL code and database tables most of the time.

We have decided to choose the Doctrine ORM, because it has YAML data fixtures load - we need it very much for our unit-tests.

The main fear I have, is that using of a new ORM framework can slow the site's performance. We can't make a shared connection between current database abstraction layer (which uses pg_connect syntax, not PDO-compatible). The database connection mechanism can't be switched to PDO-compatible, because there are lots of SQL code incompatible with PDO_SQLITE syntax.

So, as I understand it, if we will start using it, it will double the number of database connections. I'm not sure database server will be able to handle this.

What would you recommend us to do in this circumstance?

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

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

发布评论

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

评论(3

故事还在继续 2024-07-21 19:43:54

PDO_SQLITE 有什么相关性?

除非您实际上计划使用 SQLite 驱动程序,否则 PDO 不强制要求兼容性。

如果您不打算使用 SQLite,那么我将使旧数据库层 PDO 兼容并重新使用连接,直到您可以完全迁移到 Doctrine。

也就是说,连接级别并不是迁移到 ORM 时唯一关心的性能问题。 它们本质上效率低下,因此我预计查询速度较慢,应用程序服务器和数据库服务器之间的带宽使用率较高,并且由于不可避免地选择冗余数据而导致应用程序级别的内存使用率较高。
根据您当前的设置,上述问题可能会也可能不会。

不过,您可能应该对最后一段持保留态度,因为它们只是一般 ORM 的特征,而不是具体的 Doctrine,我对此没有任何经验。

Of what relevance is PDO_SQLITE?

Unless you actually plan on using the SQLite driver then compatibility is not mandated by PDO.

If you aren't going to use SQLite then I would make the legacy database layer PDO compatible and re-use the connections until you can fully migrate to Doctrine.

That said, the level of connections is not going to be your only performance concern moving to an ORM. They are inherently inefficient so I'd expect slower queries, higher bandwidth use between application servers and the database servers and higher memory use at the application level due to redundant data inevitably getting selected.
Depending on your current setup, the above may or may not be issues.

You should probably take that last paragraph with a pinch of salt though because they are just traits of ORMs in general and not Doctrine in particular, with which I've had no experience.

挽手叙旧 2024-07-21 19:43:54

您可以做的显而易见的事情就是在需要时才打开数据库连接。 我个人使用这样的代码:

public function connect() {
  if (!defined('CONNECT')) {
    mysql_connect(...);
  }
}

public function db_query($query) {
  connect();
  $ret = mysql_query($query);
  if (!$ret) {
    die(mysql_error());
    error_log(mysql_error() . ' - ' . $query);
  }
  return $ret;
}

减少重复次数并仅在需要时打开连接。

在你的情况下,你需要从一开始就分解掉最小的块。 理想情况下,它应该是一个垂直切片,这意味着该切片将使用新代码完成几乎所有数据库工作,而很少使用旧代码。 通过这种方式,您可以最少地增加数据库连接,这可以让您建立一些技能并获得一些经验。

但请注意,ORM 绝不是万能药。 您可能讨厌 SQL,并发现它繁琐且容易出错,但在大多数情况下,您只是将一组问题换成另一组问题。 我个人认为,虽然 ORM 很有用,但它被夸大了,而且比许多人意识到或愿意承认的更像是一种错误的经济。 我在 使用 ORM 或纯 SQL?

我并不是说你不应该这样做。 只是不要以为它会解决您所有的问题。 另外,由于这种重写实际上根本不会改变功能(根据您所描述的),我不确定这样做的成本是否与修复已有的内容相比更有利。 有太多的未知数,无法判断您的情况会朝哪个方向发展。

The obvious thing you can do is not open a database connection until you need it. I personally use code like this:

public function connect() {
  if (!defined('CONNECT')) {
    mysql_connect(...);
  }
}

public function db_query($query) {
  connect();
  $ret = mysql_query($query);
  if (!$ret) {
    die(mysql_error());
    error_log(mysql_error() . ' - ' . $query);
  }
  return $ret;
}

to reduce the amount of repetitive and to only open a connection when you need one.

In your case you then need to break off the smallest chunk you can to begin with. Ideally it should be a vertical slice, meaning this slice will do almost all of its database work with the new code and very little with the old. This way you can minimal doubling up of database connections and this lets you build up some skills and get some experience too.

Beware though, ORM is not by any means a panacea. You may hate SQL and find it fiddly and error prone but you are, for the most part, simply trading one set of problems for another. I personally think that while ORM can be useful it has been overhyped and is more of a false economy than many either realize or are willing to admit. I wrote more on this in Using an ORM or plain SQL?

I'm not saying you shouldn't do it. Just don't go in thinking it'll solve all your problems. Also, since this rewrite won't actually change the functionality at all (from what you've described) I'm not sure if the cost of doing so compares favourably with fixing what's there already. Too many unknowns to say which way your situation will go.

伊面 2024-07-21 19:43:54

好吧,是和否 – 只要您同时拥有非 PDO 和 PDO 连接,您的数据库连接只会增加一倍。

我不确定你的 PDO_SQLITE 参考是什么意思,因为 SQLite 是一个与你现在使用的 PostgreSQL 完全不同的数据库。

您应该能够像今天一样通过 PDO::query 运行当前查询,除非您做错了什么:)

Well, yes and no – your DB connections will only be doubled as long as you have both a non-PDO and a PDO connection.

I'm not sure what you mean with the PDO_SQLITE reference, since SQLite is a wholly different database than the PostgreSQL it seems you're using now.

You should be able to run your current queries through PDO::query just as you do today unless you are doing something very wrong :)

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