如何让 MVC 与多个数据库保持一致?
我有很多数据库可以点击并放在一页中。大多数框架只允许我为一个数据库进行两个本机配置。他们都假设该用途是用于一个新应用程序和一个数据库。我有很多遗留数据库。
由于我只能在我见过的 ci、zend 等框架中配置一个数据库,因此看起来我的额外数据库连接必须在我的控制器中进行。
我可以在后端编写什么代码以使它们远离控制器。我不想在每个不同数据库的每个控制器中放入connection=myconn 等。
另外,是否有任何框架允许 ORM 映射中的多个数据库?
I have lots of databases to hit and put in one page. Most of the frameworks only allow me two native configure them for one database. They all assume the usage is for a new app and one database. I have lots of legacy databases.
Since I can only configure one database in frameworks I have seen ci, zend, others, it looks like my additional db connections have to take place in my controller.
What could I code on the back end to keep them out of the controller. I don't want to have to put in connection=myconn, etc. in each controller for each disparate database.
Also, are there any frameworks that allow for multiple databases in the ORM mapping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过事件将控制器与数据库分开。我从控制器触发一些抽象的“SaveWhateverComplicatedData”事件,并且另一个类(知道我的所有数据存储细节)侦听这些事件并解释它们。它对我来说工作得很好,虽然我只有一个数据库,但我看不出有什么理由不能拥有更多。
这是我自己设置的自定义框架,但您可以通过将这些“数据事件”包装在看起来像数据库的界面中来连接到另一个框架。您可以使用数据事件实现框架所需的任何接口,并且仍然让您的侦听多数据库类完成实际工作。
I separate my controllers from my database with events. I fire some abstract "SaveWhateverComplicatedData" event from my controller, and a different class (who knows about all of my data storage specifics) listens for those events and interprets them. It's working quite well for me, and, though I only have one DB, I don't see any reason I couldn't have more.
This is a custom framework I've set up myself, but you could hook into another framework by wrapping these "data events" up in an interface that looks like a database. You could implement whatever interface your framework needs WITH the data events, and still have your listening multiple-database class doing the real work.