如何在codeigniter中分离数据访问层

发布于 2024-09-30 02:16:19 字数 110 浏览 0 评论 0原文

我曾经在模型本身中编写数据访问功能。现在我想将数据访问与业务逻辑分开。我使用 codeigniter 作为框架。 似乎一种方法是使用 ORM,但我猜它会带来性能损失。

有没有通用的最佳实践?

I used to write the data access functionalities in model itself. Now I want to separate data access from business logic. I am using codeigniter as framework.
It seems that one way of doing it is use ORM, but it will have a performance penalty I guess.

are there any general best practices?

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

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

发布评论

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

评论(1

潦草背影 2024-10-07 02:16:19

看看POEAA的数据源架构模式:

CodeIgniter 声称使用 ActiveRecord,但事实并非如此。它更像是一个基本的 QueryObject。要真正将 DAO 与域对象分开,您必须使用 DataMapper。根据映射需求的复杂性,您可以自己构建一个或使用 ORM。讽刺的是,PHP 世界中的大多数 ORM 都基于 ActiveRecord,这非常不适合ORM。 Doctrine 2 是我所知道的唯一使用 DataMapper 方法的方法。

ORM 总是会带来性能损失(而且可能是严重的损失)。然而,您不应该仅仅因为这个就排除 ORM。在高阻抗不匹配场景中手工制作高效的 DataMapper 可能是一项乏味且困难的工作。同样,请参阅 POEAA 以获取常见对象关系模式的列表。

似乎有一个带有 Overzealous DMZ 的 CodeIgniter 的 DataMapper 实现。我从未使用过它,也无法对此发表任何评论。它只是在快速谷歌后出现的,所以我想我将它添加到这里。

Have a look at POEAA's Data Source Architectural Patterns:

CodeIgniter claims to use ActiveRecord, but it doesnt. It's more like a rudimentary QueryObject. To truly separate your DAO from your Domain objects, you have to use a DataMapper. Depending on the complexity of your mapping needs you can build one yourself or use an ORM. Ironicaly, the majority of ORMs in the PHP World are based on ActiveRecord, which is pretty ill-suited for ORM. Doctrine 2 is the only I know that uses a DataMapper approach.

An ORM will always come with a performance penalty (and it can be a serious one). However, you should not rule out an ORM just because of that. Handcrafting an efficient DataMapper in a high impedance mismatch scenario can be tedious and difficult work. Again, see POEAA for a list of common Object-Relational patterns.

There seems to a DataMapper implementation for CodeIgniter with Overzealous DMZ. I have never worked with it and cannot say anything about it. It just came up after a quick google, so I thought I add it here.

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