colinmollenhour / mongodb-php-odm

A simple but powerful set of wrappers for using MongoDb in PHP (also a Kohana 3 module)
BSD 3-Clause "New" or "Revised" License
208 stars 50 forks source link

No way to map embedded document to PHP class. #3

Closed kirbysayshi closed 14 years ago

kirbysayshi commented 14 years ago

If you make an association using the $_references field, that object is automatically mapped using a "foreign key", and lazy loaded. This is cool, but there are many times that I want to be able to embed the object, rather than reference it, and still be able to define and manipulate that object using a PHP class.

I think that one of the benefits of using a NoSQL db like MongoDB is that embedding documents is expected, and can make things easier.

If this is possible, can you please post a quick example?

EDIT:: To further clarify, given a blog post, which would most likely have an array of comments embedded in the blog document. I would want to define the fields of a comment in a class called Comment. Each entry in the comments array would then be mapped to a Comment object, even though it's embedded.

colinmollenhour commented 14 years ago

Agreed on the utility. However, the added complexity to be able to handle things like partial updates is substantial. If you want to try to do this with the current library you could probably do so using the over-ridable after_load and before_save functions or override call or get to do it lazily. Regarding this feature request I don't think I will implement this myself since I want to keep the library simple and fast and don't really have the time. If you are using Kohana 3 I recommend you consider using Wouterr's MangoDb which implements this exact feature. If not using Kohana 3 you would need to probably modify it to make it work and include at least the Kohana_Arr class from kohana/core and possibly a few others.

http://github.com/Wouterrr/MangoDB