SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    data mapper and return type to calling object

    This is kind of a noobie question but....

    i have a set of classes that use mapper functions for data access. As a simple example, I'll have a location class and a location_mapper class.

    The main thing that the mapper class does is access the database and return "result sets". The returned result sets are often mysqli objects but increasingly, I have been returning arrays especially when the result set has an an array as a values like:
    loc['name']="some place";
    loc['phone']='312-232-2323';
    loc['contacts']=array(1=>"joe", 2=>"mike");

    So the questions I have are the following:
    1. is there a proper "return" value for arrays? ie. recordsets or arrays. I read the Fowler book and it seems like he's returning java recordsets but I think those are essentially arrays.
    2. if it's either, how would people suggest setting up those functions, sometime I create a similar function to load($loc_id) such as loadAsArray($loc_id) but I'm also starting to do load($loc_id, $return_format). Is this even reasonable design?

    thanks,

    jonathan

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would settle on returning either object or array.
    You can always use get_object_vars($object);
    Personaly i use that for transition before sending data to the template system:

    $template->setVar('location', get_object_vars(new Location(2)));

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that makes sense Findus. I'll have to look into get_object_vars. Sometimes, my objects have collections of objects (ugh....).

    My mapper classes started out as just a way to centralize sql generation but as my objects developed, there become a lot more business processing in the mapper layer. At first, I thought this would be a bad idea but it seems to function ok. I'm not that confident in my skills on this but am making a try.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We have that "problem" where i work, the mappers turns out to be _huge_ classes that handles loads of domain layer issues. Im not happy about it at all :/
    Tho its a lot easier to write the code like that, less thinking for the developer.

    I would keep it like that for starters but always keep an open door for refactoring into several classes. As it is very clean in the start, but will get messy in the long run.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •