Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Best Practices: Populating Objects from Database

by mrborisguy (Hermit)
on Nov 10, 2005 at 20:42 UTC ( [id://507532]=perlquestion: print w/replies, xml ) Need Help??

mrborisguy has asked for the wisdom of the Perl Monks concerning the following question:

I've been thinking about this for a little bit. My current context is in web programming, but I think this could be applied to a wider variety of programs.

What, in your opinions, is the best way to populate a list of objects from a database?

In the past, I've done things without classes at all, looking something like this:

while( @info = $sth->fetchrow_array ) { $f_name = $info[0]; $l_name = $info[1]; ... # do what is necessary with these variables. }

But it seems like there should be a separation of getting all of the information about what could be an object, and what to do with that information. One thought would be something like:

my @objects; while ( @info = $sth->fetchrow_array ) { my $object = Object->new(); $object->set_fname( $info[0] ); $object->set_lname( $info[1] ); ... # or my $object = Object->new( @info ); # or my $object = Object->new( fname => $info[0], lname => $info[1], ... ); push @objects, $object } for ( @objects ) { # do something with the object }

Another thought would be to create a sub to populate the objects for me. When I think about this, though, I think about two things, really...

  1. Somehow pass an SQL string to the method, but that causes a few problems: the SQL logic would have to return exactly the right fields, but the function wouldn't be able to enforce it; and it doesn't look like you can use placeholders.
    @objects = Object::populate( "SELECT fname, lname, ... FROM object WHE +RE title='programmer'" )
  2. Add some abstraction, so in the options of the subroutine there is a way to specify what objects we want to get (assuming we don't want them all). But that would undoubtedly be less flexible than using SQL.
    @objects = Object::populate( title => "programmer" );

Maybe this is a bit of a meditation, but I'm definitely looking for some monks experienced in doing things like this to slap me with some wisdom, so let me know what you've done, and what you think is practically the best solution to populating a list of objects from a database. I'm sure there are some good ways to do this. I hope my question is clear. Thanks!

    -Bryan

Replies are listed 'Best First'.
Re: Best Practices: Populating Objects from Database
by sauoq (Abbot) on Nov 10, 2005 at 20:50 UTC

    You might want to have a look at Tangram.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Best Practices: Populating Objects from Database
by lachoy (Parson) on Nov 10, 2005 at 21:32 UTC
Re: Best Practices: Populating Objects from Database
by shiza (Hermit) on Nov 10, 2005 at 20:54 UTC
Re: Best Practices: Populating Objects from Database
by phaylon (Curate) on Nov 10, 2005 at 23:07 UTC
    I'd like to add DBIx-Class to the list.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: Best Practices: Populating Objects from Database
by pajout (Curate) on Nov 11, 2005 at 15:06 UTC
    I think that it very depends on the answer of two questions: Why to store object instances in db and what kind of objects should be stored.
    For instance, if you require persistency only, you can do something as my $instance = bless($info[0],$info[1]) or eval() some pieces of this command...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://507532]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found