Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Getting HASHES out of Class::DBI

by edoc (Chaplain)
on Jan 07, 2004 at 00:05 UTC ( #319293=note: print w/replies, xml ) Need Help??


in reply to Getting HASHES out of Class::DBI

I'd have to ask, what do you need the hash for? If you really do need a hash, then yes, why are you using CDBI? Ideally you want to use the object, not the hash.

If you use CDBI objects most of the time but need the hash for a particular function, then you could add another method to your class:

Not Tested:

__PACKAGE__->set_sql( retrieve_hash => qq{ select * from __TABLE__ where id=? }, 'Main'); sub retrieve_hash { my ($class,$id) = @_; my $sth = $class->sql_retrieve_hash(); $sth->execute($id); my $hashref = $sth->fetchrow_hashref(); $sth->finish; return $hashref; }

again, though, I would recommend having a close look at your hash usage to see if it can't be modified to use the object instead.

cheers,

J

Replies are listed 'Best First'.
Re: Re: Getting HASHES out of Class::DBI
by jdtoronto (Prior) on Jan 07, 2004 at 02:18 UTC
    I'd have to ask, what do you need the hash for? If you really do need a hash, then yes, why are you using CDBI? Ideally you want to use the object, not the hash.
    Well, now you mention it. HTML::Template takes hashes, Data::FormValidate returns hashes, HTML::FillInForm takes hashes, or these tajke hashrefs, whatever. But it seems that everything I am using for validation and output wants hashes. So, indeed, why am I using Class::DBI. Class::DBI was represented as being "the way" to handle data intensive web-apps. But I am beginning to doubt that now. For small databases, few fields (some of my tables have 100-120 fields) and few relationships Class::DBI is great (my test application for example!), but as I have tried to move it into the big league here it is suffering.

    I am quite prepared for someone to tell me it is me that doesn't know how to do things, I am totally new at OO Perl! But I think I am wasting too much time trying to get CDBI shoe-horned into my app right now.

    jdtoronto

      If you are going to use objects to access a database, then Class::DBI will typically make that easier and save you code. Before I started using it, I would write my own data access objects from scratch. Class::DBI has made that unnecessary, or at least removed the boring parts of it.

      I don't think that the usefulness of Class::DBI for a particular application has much to do with the size or complexity of your database. I do think it has a lot to do with your approach to coding and whether or not you like accessing your data as objects.

      When I've used Class::DBI with HTML::Template, I found it pretty easy to copy data from a list of objects into a hash structure that H::T could handle. I definitely was writing code to handle that translation, but that's the price you pay for the strong abstraction layer that H::T provides you. If you want a templating system that doesn't require you to arrange your data like this, Template Toolkit is happy to work on Class::DBI objects directly.

        When I've used Class::DBI with HTML::Template, I found it pretty easy to copy data from a list of objects into a hash structure that H::T could handle. I definitely was writing code to handle that translation, but that's the price you pay for the strong abstraction layer that H::T provides you. If you want a templating system that doesn't require you to arrange your data like this, Template Toolkit is happy to work on Class::DBI objects directly.
        Well perrin I think you have hit it on the head. I thought again about my remarks on DB complexity. Perhaps Class::DBI is no more and no less capable of handling complex relationships than anything else. What I was complaining about was the translation, and I think a couple of suggestions have been made that I will ponder overnight.

        As always, thanks for the insightful contribution.

        John jdtoronto

        PS: Template Toolkit is on my list of things to learn when this job is complete!

      Have you tried passing the CDBI object into HTML::Template? It's just like passing in a reference to a list or hash. This is how I send database results into my templates (I'm using Petal however).

      William

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-01-24 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?