PHP MVC: Multiple databases, multiple data mappers?

落爺英雄遲暮 提交于 2020-01-17 13:57:10

问题


I am working on my HMVC project.

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.

I also use a dependency injection container (Auryn).

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

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.

My question is:

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, ...)

Thank you all for your suggestions!


回答1:


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.

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.



来源:https://stackoverflow.com/questions/44166994/php-mvc-multiple-databases-multiple-data-mappers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!