PHP MVC:多个数据库,多个数据映射器? [英] PHP MVC: Multiple databases, multiple data mappers?

查看:105
本文介绍了PHP MVC:多个数据库,多个数据映射器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做我的HMVC项目.

I am working on my HMVC project.

现在,我正在使用数据映射器,以便在模型(域对象)和MySQL数据库之间移动数据.每个映射器都将一个MySQL适配器作为依赖项.注入的适配器接收PDO实例(数据库连接)作为依赖项,并在数据库上运行sql查询.

Right now I am using data mappers in order to move data between the models (domain objects) and a MySQL database. Each mapper receives a MySQL adapter as dependency. The injected adapter receives a PDO instance (a database connection) as dependency and runs sql queries on the database.

我还使用了依赖项注入容器(Auryn).

I also use a dependency injection container (Auryn).

我希望能够同时从不同类型的存储(MySQL数据库,PostgreSQL数据库,XML提要等)中检索数据.

I'd like to be able to simultaneously retrieve data from storages of different types (MySQL database, PostgreSQL database, XML feeds, etc).

比方说,我想从 PostgreSQL数据库(通过使用 PDO 数据访问抽象)检索User数据,以进行更改,并将其保存到另一台服务器上的 MySQL数据库(通过使用 mysqli 数据访问抽象).因此,两种数据库类型都会有不同的数据访问调用.

Let's say, I want to retrieve User data from a PostgreSQL database (by using PDO data-access abstraction), to change it, and to save it into a MySQL database (by using mysqli data-access abstraction) on another server. So, there will be different data-access calls for the both database types.

我的问题是:

我应该为每种存储类型创建一个不同的映射器,例如

Should I create a different mapper for each storage type, like

UserMapperPgsql(PgsqlAdapter $adapter) 
UserMapperMySql(MySqlAdapter $adapter)

,还是我应该只创建一个具有更多适配器(每个数据类型一个)的映射器作为依存关系,例如波纹管?

, or should I create only one mapper with more adapters (one for each data type) as dependencies, like bellow?

UserMapper(PgsqlAdapter $adapter1, MySqlAdapter $adapter2, ...)

谢谢大家的建议!

推荐答案

您在那里有一个奇怪的项目.

What an odd project you have there.

无论如何.我将使用两个单独的映射器来存储单独的存储介质.因为尝试在映射器中处理这些适配器可能会变得非常复杂.

Anyway. I would go with two separate mappers for separate storage mediums. Because trying to juggle those adapters inside a mapper might end up quite complicated.

也就是说,根据持久性逻辑最终实际的复杂程度,您可能会受益于查找存储库作为简化API的方法,这将暴露给您实际完成应用程序逻辑"的位置.

That said, depending on how complicated the persistence logic actually ends up, you might benefit from looking up repositories as approach to streamline the API, that gets exposed to where your "application logic" is actually done.

这篇关于PHP MVC:多个数据库,多个数据映射器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆