SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Lithuania, Europe
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loading data for display only

    Say, you use some domain model and have Person class. You also have some Finder class which has methods to load Person objects from the db and return them as Person arrays.

    Say, you need to display all names and DOBs of people matching certain criteria ($finder->getPersonsBySomeCriteria() and output, no additional logic involved). The class Person has many more properties besides those two (weight, height etc). In this case, would you load the Person objects and set these two properties only and leave the other ones unset (or default) or would you rather return an array of arrays instead of Person objects? The array would look something like this:
    PHP Code:
    $person_properties = array(=> array('name' => 'John',
                                          
    'DOB' => '1970-02-10'),
                               
    => array('name' => 'Peter',
                                          
    'DOB' => '1975-01-10'),
                               
    => array('name' => 'James',
                                          
    'DOB' => '1875-03-14')); 
    Should efficiency be taken into consideration here?

  2. #2
    SitePoint Zealot crashmakerMX's Avatar
    Join Date
    Jun 2005
    Location
    Germany
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I would still use Person objects.

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...

    PHP Code:
    // one row
    $person -> findById);
    $html $person -> format( new Formatter() );
    // ...
    public function format$formatter ) {
    // something like this...
    return $formatter -> formatFullName$this -> export() );
    }
    // ...
    class Formatter {
    public function 
    formatFullNameIDataspace $dataspace ) {
    return 
    '<p>'.$dataspace['lastname'].', '.$dataspace['firstname'].'</p>';
    }
    // ...other methods

    On the other hand, if you have a number of rows, decorate the resultset with a ViewHelper, and pass that to the View? The ViewHelper would know how to format the data which in turn is passed over to the View it's self.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The question should not be "should i do something lightweight here?" like you are asking it, the answer to the question, and the question itself, comes naturaly once the need is there.
    Premature optimalization is bad.
    Go with Person objects untill you see/feel the need to optimalize.
    Say if you list 1k people in that list, and its displayed very often, then its probably smart to optimalize one way or another (caching or by a lighter data gatherer process).

    Dr Livingston: A finder that formats data???

  5. #5
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (Bit of a rant coming up, but I couldn't resist)

    What if you wanted a list of all persons' names, their dates of birth and the number of times that person is referenced in another table (i.e. a COUNT(..) .. GROUP BY)?

    Or a list of persons and their partners? Persons and their children?

    A Person class only cuts it when you want all the data from one table, but as soon as you start doing slightly more interesting (and complex) database queries, you can't map it onto that Person object anymore.

    Since there are many more possible cases like these, where you can't map your query results directly onto Person objects, than there are cases in which you can, maybe you should ask the question if it is such a good idea to map query results to objects after all?

    Oh, and:
    Quote Originally Posted by Dr Livingston
    $html = $person -> format( new Formatter() );
    Yeah, that's correct layering alright..
    I once had a problem.
    I thought: "Oh, I know: I'll just use XML!"
    Now I had two problems.

  6. #6
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain Proton
    (Bit of a rant coming up, but I couldn't resist)

    What if you wanted a list of all persons' names, their dates of birth and the number of times that person is referenced in another table (i.e. a COUNT(..) .. GROUP BY)?

    Or a list of persons and their partners? Persons and their children?

    A Person class only cuts it when you want all the data from one table, but as soon as you start doing slightly more interesting (and complex) database queries, you can't map it onto that Person object anymore.

    Since there are many more possible cases like these, where you can't map your query results directly onto Person objects, than there are cases in which you can, maybe you should ask the question if it is such a good idea to map query results to objects after all?
    http://www.martinfowler.com/articles/dblogic.html deals with this issue, and his practice is always to map to objects, but having the mapping flexible enough to deal with complex queries, and make sure your not repeating domain logic.

  7. #7
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Lithuania, Europe
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain Proton
    (Bit of a rant coming up, but I couldn't resist)
    What if you wanted a list of all persons' names, their dates of birth and the number of times that person is referenced in another table (i.e. a COUNT(..) .. GROUP BY)?

    Or a list of persons and their partners? Persons and their children?

    A Person class only cuts it when you want all the data from one table, but as soon as you start doing slightly more interesting (and complex) database queries, you can't map it onto that Person object anymore.

    Since there are many more possible cases like these, where you can't map your query results directly onto Person objects, than there are cases in which you can, maybe you should ask the question if it is such a good idea to map query results to objects after all?
    You could add additional properties to the Person class and/or other classes which Person objects could have instances (or instance collections) of.

    Ren: thanks for the link, I'm reading that right now, pretty interesting.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Person object doesnt have to be a mapping to only the person table. If it represents a real world consept of a Person it can include stuff like partner ($person->partnerId), or children ($person->childrenIds = array(1,4,7))

    Regarding finders, my finders only return arrays of found results (illustration):
    PHP Code:
    $people $peopleFinder->findByLastName('anderson');
    foreach (
    $people as $personId) {
        
    $peopleList->add(new Person($personId));
    }
    $peopleList->render(); 
    ...you get the idea.

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, and...

    > Yeah, that's correct layering alright..

    That is right, the layering is separate, in respect that the domain object has no presentation logic; It has however, a means to allow the presentation layer to access the domain model.


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
  •