mr groovy Posted August 19, 2009 Share Posted August 19, 2009 Hi, I am designing an implementation of the data mapper pattern, for school and to get an understanding of what is under the hood of all the ORM systems out there. The mapping itself is no to hard te get done, automaticly using relation information to extract unique objects from a resultset consisting of multiple joined tables (this is called hydration by other systems i think). These unique objects are then stored in a central space: the identitymap. When a query with joins is performed the objects can be connected: Objects with a foreignkey know exactly how to find the object they belong to, the can find it in the identitymap (this is 1:1). At the other end of the relation there is a collection (1:many) So when domainobjects are created they are added to the collections at the other end of the relation. This is my problem: Becouse the objects in the identitymap are used in the whole system, changes to these objects will have an affect everywhere. So when many querys are run (the one with many where claues the other with limit claues and so on) The collections will contain unexpected data. <?php $authorMapper = new authorMapper(); // First query: NO PROBLEMO! $authorCollection = $authorMapper->findAuthorsInnerJoinBooks(); foreach($authorCollection as $author){ // . . . foreach($author->books as $book){ // . . . } } // Second query: more specefic collections $authorCollection2 = $authorMapper->findAuthorsAndFiveNewestBooks(); foreach($authorCollection2 as $author){ // . . . foreach($author->books as $book){ /* The books collection inside the author objects would still contain ALL data from the table becouse of the first query and not the five newest books. */ } } However i may have answer to this: Just dont store collections in the identitymap. Just store objects with unique data in the identitymap and never let them refer to each other directly. And then create a decorator(s) around these raw data objects. This way the decorator can access the data in the identitymap and have its own collections at hand. Its like composing a graph of objects. What do you people say and think? does this make any sense ? and if not, why not ? Please tell me if i got some basic ideas wrong, i want your thoughts. Quote Link to comment https://forums.phpfreaks.com/topic/170919-mapping-to-a-database/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.