MySQL和Mongodb在一个应用程序中的混合 [英] Mix of MySQL and Mongodb in an application

查看:701
本文介绍了MySQL和Mongodb在一个应用程序中的混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP / Symfony2 / Doctrine2编写一个Web应用程序,并完成数据库的设计。我们必须将这些对象(例如项目,供应商)导入到来自具有各种领域的不同客户的数据库中。有些客户在项目对象中有2个字段,有的则有20个字段,所以我在考虑在MongoDB中实现它们,因为它似乎很好用。



Symfony2支持ORM和ODM,所以不应该是一个问题,现在我的问题是如何确保两个数据库中数据的完整性。因为我的MySQL数据库中的对象需要以某种方式链接到MongoDB 中的对象,以获取完整性问题。



有没有更好的解决方案?任何帮助/想法将不胜感激

解决方案

Bulat 在OpenSky中实施了一个Doctrine扩展,用于处理MongoDB文档和MySQL记录之间的引用,该记录目前位于他们的(已经过时的) DoctrineExtensions 项目的rel =noreferrer> fork 的openski / DoctrineExtensions。您将要查看 orm2odm_references openskyfork 分支。为了在您的项目中可以使用它,您可能希望将其移植到DoctrineExtensions的新叉上,或者将代码简单地包含在应用程序中。不幸的是,除了代码本身,没有文档。



幸运的是,还有一个食谱文章在Doctrine网站上介绍如何从头开始实施。基本上,您依靠事件侦听器将其属性替换为其他对象管理器中的引用(即未初始化的Proxy对象),并且Proxy对象的自然行为以延迟加载自身处理其余部分。如果事件监听器是一个服务,您可以轻松地将ORM和ODM对象管理器注入到其中。



此模型保证的唯一完整性是您将收到尝试水合一个不良参考的例外,这可能比您通过简单地存储另一个数据库的ID并手动查询获得的更多。


I'm writing a web application using PHP/Symfony2/Doctrine2 and just finishing up the design of the Database. We have to import these objects (for ex. Projects, Vendors) into our database that come from different customers with variety of fields. Some customers have 2 fields in the project object and some have 20. So I was thinking about implementing them in MongoDB since it seems like a good use for it.

Symfony2 supports both ORM and ODM so that shouldn't be a problem, Now my question is how to ensure the integrity of the data in both databases. Because Objects in my MySQL db need to somehow be linked to the objects in the MongoDB for integrity issues.

Are there any better solutions out there? Any help/thoughts would be appreciated

解决方案

Bulat implemented a Doctrine extension while we were at OpenSky for handling references between MongoDB documents and MySQL records, which is currently sitting in their (admittedly outdated) fork of the DoctrineExtensions project. You'll want to look at either the orm2odm_references or openskyfork branches. For this to be usable in your project, you'll probably want to port it over to a fresh fork of DoctrineExtensions, or simply incorporate the code into your application. Unfortunately, there is no documentation apart from the code itself.

Thankfully, there is also cookbook article on the Doctrine website that describes how to implement this from scratch. Basically, you rely on an event listener to replace your property with a reference (i.e. uninitialized Proxy object) from the other object manager and the natural behavior of Proxy objects to lazily load themselves takes care of the rest. Provided the event listener is a service, you can easily inject both the ORM and ODM object managers into it.

The only integrity guaranteed by this model is that you'll receive exceptions when trying to hydrate a bad reference, which is probably more than you'd get by simply storing an ID of the other database and querying manually.

这篇关于MySQL和Mongodb在一个应用程序中的混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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