Hi there,

First of all thanks to all those who replied to my previous posts of Using DBI: handlers or references to handlers? and OO Perl: calling a constructor within a class. After taking your advice my application is now maturing well.

I have come across a new dilemma however. My application has a number of users, and each user can control several projects. So I have a User class and a Project class.

There is a backend database to all this with user and project tables. I am struggling to find the best way to get the data out of the database and into the objects.

An example: users log into the system using login.pl. This takes a username, retrieves the user's details from the database and makes a new User object.

I have tried the following methods for populating the User object with the details:

- hardcode the SQL into login.pl, retrieve an array, and pass that array to the new() constructor of User

- write a class method for User which retrieves an array of data and calls the constructor

- write a new() constructor that queries the database

- write a Database package/class which contains several functions for retrieving values, which are then fed to a new() constructor for User

- write a User::Database class for specific user-type queries on the database

However, I'm confused as to the 'best' way to do this. Everytime I come up with a solution I seem to find a big problem with it. Another consideration I have is that as each user has several projects, then each User object has to have the capability to store an array of Project objects. Should I just do one big retrieve when I create a User object, or do I create the array of Project objects when needed? This confused me even more when writing the constructors.

Any pointers are most welcome.

Thanks,

fx


In reply to OO Perl: classes and database access by fx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.